forked from Bush2021/chrome_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputhook.cc
More file actions
85 lines (66 loc) · 2.22 KB
/
Copy pathinputhook.cc
File metadata and controls
85 lines (66 loc) · 2.22 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "inputhook.h"
#include <windows.h>
#include <algorithm>
#include <vector>
#include "utils.h"
namespace {
template <typename Handler>
struct HandlerEntry {
Handler handler;
int priority;
};
std::vector<HandlerEntry<KeyboardHandler>> keyboard_handlers;
std::vector<HandlerEntry<MouseHandler>> mouse_handlers;
HHOOK keyboard_hook = nullptr;
HHOOK mouse_hook = nullptr;
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
for (const auto& entry : keyboard_handlers) {
if (entry.handler(wParam, lParam)) {
return 1;
}
}
}
return CallNextHookEx(keyboard_hook, nCode, wParam, lParam);
}
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode != HC_ACTION) {
return CallNextHookEx(mouse_hook, nCode, wParam, lParam);
}
if (wParam == WM_MOUSEMOVE || wParam == WM_NCMOUSEMOVE) {
return CallNextHookEx(mouse_hook, nCode, wParam, lParam);
}
PMOUSEHOOKSTRUCT pmouse = reinterpret_cast<PMOUSEHOOKSTRUCT>(lParam);
if (pmouse->dwExtraInfo == GetMagicCode()) {
return CallNextHookEx(mouse_hook, nCode, wParam, lParam);
}
for (const auto& entry : mouse_handlers) {
if (entry.handler(wParam, lParam)) {
return 1;
}
}
return CallNextHookEx(mouse_hook, nCode, wParam, lParam);
}
} // namespace
void RegisterKeyboardHandler(KeyboardHandler handler,
HandlerPriority priority) {
keyboard_handlers.emplace_back(std::move(handler), static_cast<int>(priority));
std::ranges::sort(keyboard_handlers, [](const auto& a, const auto& b) {
return a.priority < b.priority;
});
}
void RegisterMouseHandler(MouseHandler handler, HandlerPriority priority) {
mouse_handlers.emplace_back(std::move(handler), static_cast<int>(priority));
std::ranges::sort(mouse_handlers, [](const auto& a, const auto& b) {
return a.priority < b.priority;
});
}
bool IsKeyPressed(int vk) {
return vk && (::GetKeyState(vk) & 0x8000) != 0;
}
void InstallInputHooks() {
keyboard_hook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, hInstance,
GetCurrentThreadId());
mouse_hook =
SetWindowsHookEx(WH_MOUSE, MouseProc, hInstance, GetCurrentThreadId());
}