forked from seven-mile/ScriptHookWar3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard_manager.cpp
50 lines (42 loc) · 1.07 KB
/
keyboard_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "pch.h"
#include "keyboard_manager.h"
#include <ranges>
using tick_t = decltype(GetTickCount64());
struct hotkey_struct
{
int vk;
bool ctrl, alt, shift;
std::function<void()> handler;
tick_t last_tick;
};
static size_t cntHotkeys = 0;
static std::map<hotkey_t, hotkey_struct> hotkeys;
hotkey_t KeyboardManager::Register(
const std::function<void()>& handler,
int vk, bool ctrl, bool alt, bool shift)
{
hotkeys[++cntHotkeys] = hotkey_struct{ vk, ctrl, alt, shift, handler };
return cntHotkeys;
}
void KeyboardManager::Unregister(hotkey_t hotkey)
{
hotkeys.erase(hotkey);
}
inline bool IsKeyDown(int code) {
return GetAsyncKeyState(code) & (1u << 31);
}
void KeyboardManager::Check(int noRespTimeInMs)
{
auto curTick = GetTickCount64();
for (auto& hotkey : hotkeys | std::views::values)
{
if (hotkey.last_tick + noRespTimeInMs < curTick
&& IsKeyDown(hotkey.vk)
&& (!hotkey.ctrl || IsKeyDown(VK_CONTROL))
&& (!hotkey.alt || IsKeyDown(VK_MENU))
&& (!hotkey.shift || IsKeyDown(VK_SHIFT)))
{
hotkey.handler();
}
}
}