-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrollhook.cpp
More file actions
312 lines (288 loc) · 8.78 KB
/
scrollhook.cpp
File metadata and controls
312 lines (288 loc) · 8.78 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#define UNICODE
#define _UNICODE
#define NOMINMAX
#include <Windows.h>
#include <utility>
#include <cstdio>
#include <list>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <string_view>
#pragma comment(lib, "User32.lib")
#pragma comment(lib, "Kernel32.lib")
const DWORD defaultScanCode = 0x56;
const char *const defaultName = "ISO extra '\\'";
const DWORD defaultPrecisionScanCode = 0x3B; // F1
const DWORD defaultHorizPrecisionScanCode = 0x3C; // F2
DWORD mainThreadId;
HHOOK hkKeybd = nullptr;
HHOOK hkMouse = nullptr;
bool swapMouseScroll = false;
bool precisionMode = false;
bool horizPrecisionMode = false;
std::queue<INPUT, std::list<INPUT>> *inputQueue;
std::mutex *mutex;
std::condition_variable *cond;
bool badCommandLine = false;
bool helpRequested = false;
bool verbose = false;
bool showKeyCodes = false;
bool swapScrollDirection = false;
DWORD targetScanCode = defaultScanCode;
DWORD precisionScanCode = defaultPrecisionScanCode;
DWORD horizPrecisionScanCode = defaultHorizPrecisionScanCode;
template<class F>
struct ScopeExit {
F f;
~ScopeExit() {
f();
}
};
template<class F>
ScopeExit<F> scope_exit(F f) {
return ScopeExit<F>{std::move(f)};
}
void inputThread() {
std::unique_lock<std::mutex> lock{*mutex};
if (verbose) printf("Input queue started.\n");
while (true) {
cond->wait(lock);
if (inputQueue->empty()) {
if (verbose) printf("Input queue will shut down.\n");
lock.unlock();
cond->notify_one();
return;
}
int count = 0;
do {
auto &input = inputQueue->front();
lock.unlock();
SendInput(1, &input, sizeof(INPUT));
++count;
lock.lock();
inputQueue->pop();
} while (!inputQueue->empty());
if (verbose) printf("Input queue processed %d event(s).\n", count);
}
}
LRESULT CALLBACK KeybdLL(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode < 0) {
return CallNextHookEx(hkKeybd, nCode, wParam, lParam);
}
auto &info = *reinterpret_cast<KBDLLHOOKSTRUCT *>(lParam);
switch (wParam) {
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (showKeyCodes) {
printf("Key down: VK = %ld; Scan = 0x%lX.\n",
info.vkCode, info.scanCode);
break;
}
if (info.scanCode == targetScanCode) {
if (!swapMouseScroll) {
if (verbose) printf("Toggle key down.\n");
swapMouseScroll = true;
precisionMode = false;
horizPrecisionMode = false;
}
return true;
} else if (info.scanCode == precisionScanCode) {
if (!swapMouseScroll) {
// If swapMouseScroll is false, this variable means "do we need to
// eat the key up message" - aka was the key pressed while
// swapMouseScroll was active.
precisionMode = false;
} else if (!precisionMode) {
if (verbose) printf("Switching to precision mode.\n");
precisionMode = true;
horizPrecisionMode = false;
return true;
}
} else if (info.scanCode == horizPrecisionScanCode) {
if (!swapMouseScroll) {
horizPrecisionMode = false;
} else if (!horizPrecisionMode) {
if (verbose) printf("Switching to horizontal precision mode.\n");
horizPrecisionMode = true;
precisionMode = false;
return true;
}
}
break;
case WM_KEYUP:
case WM_SYSKEYUP:
if (showKeyCodes) {
break;
}
if (info.scanCode == targetScanCode) {
if (verbose) printf("Toggle key up.\n");
swapMouseScroll = false;
return true;
} else if (info.scanCode == precisionScanCode && precisionMode) {
if (verbose) printf("Ate precision mode key up.\n");
if (!swapMouseScroll) {
// Eat the first key up message but pass later ones on.
precisionMode = false;
}
return true;
} else if (info.scanCode == horizPrecisionScanCode && horizPrecisionMode) {
if (verbose) printf("Ate horizontal precision mode key up.\n");
if (!swapMouseScroll) {
horizPrecisionMode = false;
}
return true;
}
break;
default:
break;
}
return CallNextHookEx(hkKeybd, nCode, wParam, lParam);
}
LRESULT CALLBACK MouseLL(int nCode, WPARAM wParam, LPARAM lParam) {
auto &info = *reinterpret_cast<MSLLHOOKSTRUCT *>(lParam);
if (nCode < 0 || showKeyCodes) {
return CallNextHookEx(hkMouse, nCode, wParam, lParam);
}
switch (wParam) {
case WM_MOUSEWHEEL:
if (info.flags & LLMHF_INJECTED) { break; }
if (swapMouseScroll) {
INPUT input;
input.type = INPUT_MOUSE;
input.mi.dx = info.pt.x;
input.mi.dy = info.pt.y;
input.mi.time = info.time;
input.mi.dwExtraInfo = 0;
if (precisionMode) {
if ((long)info.mouseData > 0) {
input.mi.mouseData = 40;
} else {
input.mi.mouseData = (DWORD)-40;
}
input.mi.dwFlags = MOUSEEVENTF_WHEEL | MOUSEEVENTF_ABSOLUTE;
} else {
if (swapScrollDirection) {
input.mi.mouseData = (DWORD)((long)info.mouseData >> 16);
} else {
input.mi.mouseData = (DWORD)(-((long)info.mouseData >> 16));
}
input.mi.dwFlags = MOUSEEVENTF_HWHEEL | MOUSEEVENTF_ABSOLUTE;
}
{
std::lock_guard<std::mutex> guard{*mutex};
inputQueue->emplace(input);
}
cond->notify_one();
return true;
}
break;
default:
break;
}
return CallNextHookEx(hkMouse, nCode, wParam, lParam);
}
BOOL WINAPI CtrlHandler(DWORD event) {
switch (event) {
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
if (verbose) printf("Posting quit message.\n");
PostThreadMessage(mainThreadId, WM_QUIT, 0, 0);
break;
default:
break;
}
return true;
}
void parseCommandLine(int argc, const wchar_t *const argv[]) {
for (int i = 1; i < argc; ++i) {
std::wstring_view arg{argv[i]};
if (arg == L"-h") {
helpRequested = true;
} else if (arg == L"-v") {
verbose = true;
} else if (arg == L"-s") {
showKeyCodes = true;
} else if (arg == L"-d") {
swapScrollDirection = true;
} else {
long code;
wchar_t *endptr;
code = wcstol(argv[i], &endptr, 16);
if ((endptr - argv[i]) != arg.size()) {
printf("Invalid number '%ls'.\n", argv[i]);
badCommandLine = true;
} else {
targetScanCode = (DWORD)code;
}
}
}
}
void showHelp() {
printf("Usage: scrollhook <options> <scan-code>\n");
printf("Scan code specified in hex (no 0x).\n");
printf("Default scan code = 0x%lX (%s).\n", defaultScanCode, defaultName);
printf("Options:\n");
printf(" -h Show help.\n");
printf(" -d Swap scroll direction.\n");
printf(" -s Show scan codes instead of modifying input.\n");
printf(" -v Verbose logging.\n");
}
int wmain(int argc, wchar_t *argv[]) {
HINSTANCE hMod = GetModuleHandle(nullptr);
mainThreadId = GetCurrentThreadId();
parseCommandLine(argc, argv);
if (helpRequested || badCommandLine) {
showHelp();
return badCommandLine ? 1 : 0;
}
SetConsoleCtrlHandler(nullptr, false);
SetConsoleCtrlHandler(CtrlHandler, true);
inputQueue = new std::queue<INPUT, std::list<INPUT>>();
mutex = new std::mutex();
cond = new std::condition_variable();
if (!(hkKeybd = SetWindowsHookEx(WH_KEYBOARD_LL, &KeybdLL, hMod, 0))) {
return GetLastError();
}
if (!(hkMouse = SetWindowsHookEx(WH_MOUSE_LL, &MouseLL, hMod, 0))) {
return GetLastError();
}
if (verbose) printf("Hooks registered for scan code 0x%lX, precision code 0x%lX, horizontal precision code 0x%lX.\n",
targetScanCode, precisionScanCode, horizPrecisionScanCode);
std::thread inputQueueThread{&inputThread};
auto iqfree = scope_exit([&]() {
if (verbose) printf("Begin input queue shutdown.\n");
{
std::lock_guard<std::mutex> guard(*mutex);
while (!inputQueue->empty()) {
inputQueue->pop();
}
}
if (verbose) printf("Waiting for input queue to finish.\n");
cond->notify_one();
{
std::unique_lock<std::mutex> guard(*mutex);
cond->wait(guard);
delete inputQueue;
}
delete mutex;
delete cond;
inputQueueThread.join();
if (verbose) printf("Input queue shut down finished.\n");
});
auto on_exit = scope_exit([&]() {
if (hkMouse) UnhookWindowsHookEx(hkMouse);
if (hkKeybd) UnhookWindowsHookEx(hkKeybd);
if (verbose) printf("Hooks unregistered.\n");
});
if (verbose) printf("Message queue starting.\n");
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0) > 0) {
DispatchMessage(&msg);
}
if (verbose) printf("Message queue ended.\n");
return msg.wParam;
}