-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnew_hook.h
More file actions
161 lines (137 loc) · 5.12 KB
/
new_hook.h
File metadata and controls
161 lines (137 loc) · 5.12 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifndef HOOK_H
#define HOOK_H
#include <vector>
#include <windows.h>
#define DEBUG
#ifdef DEBUG
#include <iostream>
#define dbg(x) x
#else
#define dbg(x)
#endif
struct Event {
// if `key` = 0, then `direction` won't be populated
// otherwise, `point` won't be populated
int key{ 0 }; // virtual key code, `WM_MOUSEMOVE` for mouse movement, `WM_MOUSEWHEEL` for mouse wheel scroll
bool direction{ false }; // false for key release and scroll up, true for key press and scroll down
bool injected{ false }; // false for physical input, true for injected input
POINT point{ 0, 0 }; // if `key` = 0, gives the position of cursor
};
namespace Hook {
const int BLOCK{ 0 };
const int PROPAGATE{ 1 };
const int INTERRUPT{ 2 };
namespace {
bool states[256] = { 0 };
int (*function)(const Event&){ nullptr };
Event event;
bool run_function() {
if (event.key != WM_MOUSEMOVE && event.key != WM_MOUSEWHEEL) {
states[event.key] = event.direction;
states[VK_SHIFT] = states[VK_LSHIFT] || states[VK_RSHIFT];
states[VK_CONTROL] = states[VK_LCONTROL] || states[VK_RCONTROL];
states[VK_MENU] = states[VK_LMENU] || states[VK_RMENU];
}
int action = function(event);
if (action == INTERRUPT) {
PostQuitMessage(0);
}
return action == PROPAGATE;
}
LRESULT CALLBACK keyboardHook(int nCode, WPARAM wParam, LPARAM lParam) {
static PKBDLLHOOKSTRUCT keyboard{ (PKBDLLHOOKSTRUCT)lParam };
event.key = keyboard->vkCode;
event.direction = !(keyboard->flags & LLKHF_UP);
event.injected = keyboard->flags & LLKHF_INJECTED;
return run_function() ? CallNextHookEx(NULL, nCode, wParam, lParam) : 1;
}
LRESULT CALLBACK mouseHook(int nCode, WPARAM wParam, LPARAM lParam) {
static PMSLLHOOKSTRUCT mouse{ (PMSLLHOOKSTRUCT)lParam };
event.injected = mouse->flags;
switch (wParam) {
case WM_MOUSEMOVE:
event.key = WM_MOUSEMOVE;
event.point = mouse->pt;
break;
case WM_MOUSEWHEEL:
event.key = WM_MOUSEWHEEL;
event.direction = mouse->mouseData == 0xff880000;
break;
case WM_LBUTTONDOWN:
event.key = VK_LBUTTON;
event.direction = true;
break;
case WM_LBUTTONUP:
event.key = VK_LBUTTON;
event.direction = false;
break;
case WM_RBUTTONDOWN:
event.key = VK_RBUTTON;
event.direction = true;
break;
case WM_RBUTTONUP:
event.key = VK_RBUTTON;
event.direction = false;
break;
case WM_MBUTTONDOWN:
event.key = VK_MBUTTON;
event.direction = true;
break;
case WM_MBUTTONUP:
event.key = VK_MBUTTON;
event.direction = false;
break;
case WM_XBUTTONDOWN:
case WM_XBUTTONUP:
event.key = mouse->mouseData >> 16 == 1 ? VK_XBUTTON1 : VK_XBUTTON2;
event.direction = wParam == WM_XBUTTONDOWN;
break;
default:
dbg(std::cout << "default???\n");
}
run_function();
return run_function() ? CallNextHookEx(NULL, nCode, wParam, lParam) : 1;
}
}
void run(int (*new_function)(const Event&)) {
dbg(std::cout << "Start run\n");
HHOOK hKeyboardHook{ SetWindowsHookEx(WH_KEYBOARD_LL, keyboardHook, 0, 0) };
HHOOK hMouseHook { SetWindowsHookEx(WH_MOUSE_LL , mouseHook , 0, 0) };
function = new_function;
GetKeyboardState((PBYTE)states);
MSG msg;
dbg(std::cout << "Entering loop\n");
while (GetMessage(&msg, NULL, 0, 0));
dbg(std::cout << "Unhooking\n");
UnhookWindowsHookEx(hKeyboardHook);
UnhookWindowsHookEx(hMouseHook);
dbg(std::cout << "End run\n");
}
// type = false for physical state, type = true for virtual state
bool state(int key, bool type = false) {
if (key >= 0 && key <= 256) {
if (type) {
return GetKeyState(key) | 0x8000;
}
return states[key];
}
return false;
}
bool states_or(const std::vector<int>& keys, bool type = false) {
for (int key : keys) {
if (state(key, type)) {
return true;
}
}
return false;
}
bool states_and(const std::vector<int>& keys, bool type = false) {
for (int key : keys) {
if (!state(key, type)) {
return false;
}
}
return true;
}
};
#endif