Repository navigation
Expand file tree
/
Copy pathInputHook.cpp
More file actions
461 lines (426 loc) · 22.3 KB
/
Copy pathInputHook.cpp
File metadata and controls
461 lines (426 loc) · 22.3 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include "PepperSnapInternal.h"
// ----------------------------------------------------------------------------
// Custom Hotkeys, Explorer Image Context Menu & Low-Level Keyboard Hook
// ----------------------------------------------------------------------------
int g_RecordingHotkeySlot = 0; // 0 = None, 1 = Custom Window, 2 = Instant Fullscreen, 3 = Instant Save Previous Window
DWORD g_LastHotkeyTriggerTick = 0;
// Set when a PrintScreen key-down has already been handled by the hook proc below. The
// key-up fallback there exists for keyboards and games that only ever deliver the release,
// so when the down was seen there is nothing left for the up to do - and firing it anyway
// is what saved one press of Instant fullscreen twice.
static bool g_SnapshotHandledOnKeyDown = false;
std::wstring PepperSnapDaemon::FormatHotkeyString(const HotkeyBinding& hk) {
if (hk.vk == 0) return L"";
std::wstring out;
if (hk.modifiers & MOD_CONTROL) out += L"Ctrl + ";
if (hk.modifiers & MOD_SHIFT) out += L"Shift + ";
if (hk.modifiers & MOD_ALT) out += L"Alt + ";
if (hk.modifiers & MOD_WIN) out += L"Win + ";
switch (hk.vk) {
case VK_ESCAPE: out += L"Esc"; break;
case VK_SNAPSHOT: out += L"PrintScreen"; break;
case VK_SPACE: out += L"Space"; break;
case VK_TAB: out += L"Tab"; break;
case VK_RETURN: out += L"Enter"; break;
case VK_BACK: out += L"Backspace"; break;
case VK_INSERT: out += L"Insert"; break;
case VK_DELETE: out += L"Delete"; break;
case VK_HOME: out += L"Home"; break;
case VK_END: out += L"End"; break;
case VK_PRIOR: out += L"PageUp"; break;
case VK_NEXT: out += L"PageDown"; break;
case VK_PAUSE: out += L"Pause"; break;
case VK_UP: out += L"Up"; break;
case VK_DOWN: out += L"Down"; break;
case VK_LEFT: out += L"Left"; break;
case VK_RIGHT: out += L"Right"; break;
case VK_OEM_MINUS:
case VK_SUBTRACT: out += L"-"; break;
case VK_OEM_PLUS:
case VK_ADD: out += L"="; break;
default:
if (hk.vk >= 'A' && hk.vk <= 'Z') {
out.push_back((wchar_t)hk.vk);
} else if (hk.vk >= '0' && hk.vk <= '9') {
out.push_back((wchar_t)hk.vk);
} else if (hk.vk >= VK_F1 && hk.vk <= VK_F24) {
out += L"F" + std::to_wstring(hk.vk - VK_F1 + 1);
} else {
UINT scan = MapVirtualKeyW(hk.vk, MAPVK_VK_TO_VSC);
WCHAR keyName[64] = {0};
if (GetKeyNameTextW((LONG)(scan << 16), keyName, 63) > 0) {
out += keyName;
} else {
out += L"Key(" + std::to_wstring(hk.vk) + L")";
}
}
break;
}
return out;
}
std::wstring FormatLabelWithShortcut(const std::wstring& label, const HotkeyBinding& hk, const std::wstring& suffix) {
if (hk.vk == 0) return label + suffix;
return label + L" (" + PepperSnapDaemon::FormatHotkeyString(hk) + L")" + suffix;
}
bool MatchesOverlayShortcut(const HotkeyBinding& hk, UINT vk, bool ctrl, bool shift, bool alt) {
if (hk.vk == 0) return false;
bool vkMatch = (vk == hk.vk) ||
(hk.vk == VK_OEM_MINUS && vk == VK_SUBTRACT) ||
(hk.vk == VK_OEM_PLUS && vk == VK_ADD) ||
(hk.vk == '0' && vk == VK_NUMPAD0);
if (!vkMatch) return false;
bool reqCtrl = (hk.modifiers & MOD_CONTROL) != 0;
bool reqShift = (hk.modifiers & MOD_SHIFT) != 0;
bool reqAlt = (hk.modifiers & MOD_ALT) != 0;
return (ctrl == reqCtrl) && (shift == reqShift) && (alt == reqAlt);
}
bool MatchesOverlayShortcutWithOptionalShift(const HotkeyBinding& hk, UINT vk, bool ctrl, bool shift, bool alt) {
if (hk.vk == 0 || vk == 0) return false;
bool vkMatch = (vk == hk.vk) ||
(hk.vk == VK_OEM_MINUS && vk == VK_SUBTRACT) ||
(hk.vk == VK_OEM_PLUS && vk == VK_ADD) ||
(hk.vk == '0' && vk == VK_NUMPAD0);
if (!vkMatch) return false;
bool reqCtrl = (hk.modifiers & MOD_CONTROL) != 0;
bool reqShift = (hk.modifiers & MOD_SHIFT) != 0;
bool reqAlt = (hk.modifiers & MOD_ALT) != 0;
if (ctrl != reqCtrl || alt != reqAlt) return false;
return (shift == reqShift) || (shift && !reqShift);
}
bool MatchesAnyPinnedShortcut(UINT vk, bool ctrl, bool shift, bool alt) {
return MatchesOverlayShortcutWithOptionalShift(g_Daemon.hkPinZoomOut, vk, ctrl, shift, alt) ||
MatchesOverlayShortcutWithOptionalShift(g_Daemon.hkPinZoomIn, vk, ctrl, shift, alt) ||
MatchesOverlayShortcutWithOptionalShift(g_Daemon.hkPinZoomReset, vk, ctrl, shift, alt) ||
MatchesOverlayShortcutWithOptionalShift(g_Daemon.hkPinPosReset, vk, ctrl, shift, alt) ||
MatchesOverlayShortcutWithOptionalShift(g_Daemon.hkPinOutline, vk, ctrl, shift, alt) ||
MatchesOverlayShortcutWithOptionalShift(g_Daemon.hkPinHideToolbar, vk, ctrl, shift, alt) ||
MatchesOverlayShortcutWithOptionalShift(g_Daemon.hkPinSmooth, vk, ctrl, shift, alt) ||
MatchesOverlayShortcutWithOptionalShift(g_Daemon.hkPinSaveAs, vk, ctrl, shift, alt) ||
MatchesOverlayShortcutWithOptionalShift(g_Daemon.hkPinSave, vk, ctrl, shift, alt) ||
MatchesOverlayShortcutWithOptionalShift(g_Daemon.hkPinCopy, vk, ctrl, shift, alt) ||
MatchesOverlayShortcut(g_Daemon.hkPinSearchGoogle, vk, ctrl, shift, alt) ||
MatchesOverlayShortcutWithOptionalShift(g_Daemon.hkPinClose, vk, ctrl, shift, alt);
}
void PepperSnapDaemon::ApplyGlobalHotkeys() {
if (!hTrayWnd) return;
UnregisterHotKey(hTrayWnd, HK_REGION_SNIP);
UnregisterHotKey(hTrayWnd, HK_FULL_SNAP);
UnregisterHotKey(hTrayWnd, HK_PREV_REGION);
UnregisterHotKey(hTrayWnd, HK_PIN_CLIPBOARD);
UnregisterHotKey(hTrayWnd, HK_EDIT_CLIPBOARD);
UnregisterHotKey(hTrayWnd, HK_SEARCH_GOOGLE);
auto regHk = [&](int id, const HotkeyBinding& hk) {
if (hk.vk == 0) return;
if (!RegisterHotKey(hTrayWnd, id, hk.modifiers | 0x4000 /*MOD_NOREPEAT*/, hk.vk)) {
RegisterHotKey(hTrayWnd, id, hk.modifiers, hk.vk);
}
};
regHk(HK_REGION_SNIP, hkRegionSnip);
regHk(HK_FULL_SNAP, hkFullSnap);
regHk(HK_PREV_REGION, hkPrevRegion);
regHk(HK_PIN_CLIPBOARD, hkPinClipboard);
regHk(HK_EDIT_CLIPBOARD, hkEditClipboard);
regHk(HK_SEARCH_GOOGLE, hkSearchGoogle);
UpdateTrayTooltip();
}
bool IsModifierVk(DWORD vk) {
return vk == VK_CONTROL || vk == VK_LCONTROL || vk == VK_RCONTROL ||
vk == VK_SHIFT || vk == VK_LSHIFT || vk == VK_RSHIFT ||
vk == VK_MENU || vk == VK_LMENU || vk == VK_RMENU ||
vk == VK_LWIN || vk == VK_RWIN;
}
UINT GetCurrentHardwareModifiers() {
UINT curMods = 0;
if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0 ||
(GetAsyncKeyState(VK_LCONTROL) & 0x8000) != 0 ||
(GetAsyncKeyState(VK_RCONTROL) & 0x8000) != 0) {
curMods |= MOD_CONTROL;
}
if ((GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0 ||
(GetAsyncKeyState(VK_LSHIFT) & 0x8000) != 0 ||
(GetAsyncKeyState(VK_RSHIFT) & 0x8000) != 0) {
curMods |= MOD_SHIFT;
}
if ((GetAsyncKeyState(VK_MENU) & 0x8000) != 0 ||
(GetAsyncKeyState(VK_LMENU) & 0x8000) != 0 ||
(GetAsyncKeyState(VK_RMENU) & 0x8000) != 0) {
curMods |= MOD_ALT;
}
if ((GetAsyncKeyState(VK_LWIN) & 0x8000) != 0 ||
(GetAsyncKeyState(VK_RWIN) & 0x8000) != 0) {
curMods |= MOD_WIN;
}
return curMods;
}
static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
KBDLLHOOKSTRUCT* kb = (KBDLLHOOKSTRUCT*)lParam;
if ((kb->flags & LLKHF_INJECTED) != 0) {
return CallNextHookEx(g_Daemon.hKeyHook, nCode, wParam, lParam);
}
bool isKeyDown = (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN);
bool isKeyUp = (wParam == WM_KEYUP || wParam == WM_SYSKEYUP);
// Normalize SysRq (scanCode 0x54 when Shift/Alt + PrintScreen is pressed) to VK_SNAPSHOT
DWORD vk = kb->vkCode;
if (kb->scanCode == 0x54 || kb->scanCode == 0x37) {
if (vk == VK_SNAPSHOT || vk == VK_EXECUTE || vk == 0xFF) {
vk = VK_SNAPSHOT;
}
}
// A press of PrintScreen arrives here twice: key-down, then key-up. The key-up
// fallback further below exists only for keyboards and games that never deliver the
// key-down (many swallow Shift+PrintScreen and emit the release alone), so once the
// down has been handled the up must not fire a second capture - the first capture is
// usually still running, and the tray's debounce only spans its beginning.
// Checked here rather than next to that fallback so the flag is cleared on every
// release, whichever branch above happens to consume it.
if (isKeyUp && vk == VK_SNAPSHOT) {
bool handledOnDown = g_SnapshotHandledOnKeyDown;
g_SnapshotHandledOnKeyDown = false;
if (handledOnDown) {
return CallNextHookEx(g_Daemon.hKeyHook, nCode, wParam, lParam);
}
}
// If the Customize Keys dialog is actively recording a custom shortcut key
HWND hRecTarget = (g_Daemon.hCustomizeKeysWnd && IsWindow(g_Daemon.hCustomizeKeysWnd))
? g_Daemon.hCustomizeKeysWnd
: ((g_Daemon.hOptionsWnd && IsWindow(g_Daemon.hOptionsWnd)) ? g_Daemon.hOptionsWnd : nullptr);
if (g_RecordingHotkeySlot != 0 && hRecTarget) {
if (isKeyDown || (isKeyUp && vk == VK_SNAPSHOT)) {
if (isKeyDown && vk == VK_ESCAPE) {
PostMessageW(hRecTarget, WM_SHORTCUT_RECORDED, 0, VK_ESCAPE);
return 1;
}
if (!IsModifierVk(vk)) {
UINT mods = GetCurrentHardwareModifiers();
if (kb->flags & LLKHF_ALTDOWN) mods |= MOD_ALT;
PostMessageW(hRecTarget, WM_SHORTCUT_RECORDED, (WPARAM)mods, (LPARAM)vk);
return 1;
}
}
if (vk == VK_SNAPSHOT) {
return 1;
}
return CallNextHookEx(g_Daemon.hKeyHook, nCode, wParam, lParam);
}
bool noModalOpen =
(!g_Daemon.hOptionsWnd || !IsWindow(g_Daemon.hOptionsWnd)) &&
(!g_Daemon.hCustomizeKeysWnd || !IsWindow(g_Daemon.hCustomizeKeysWnd)) &&
(!g_Daemon.hNamingSyntaxWnd || !IsWindow(g_Daemon.hNamingSyntaxWnd)) &&
(!g_Daemon.hShortcutsWnd || !IsWindow(g_Daemon.hShortcutsWnd)) &&
!g_Daemon.isSavingAsModalOpen;
// Handle key-down for all global capture hotkeys, plus key-up fallback for VK_SNAPSHOT
// (Many keyboards/games swallow WM_KEYDOWN for Shift+PrintScreen and only emit WM_KEYUP).
// Evaluated before overlay key forwarding so capture shortcuts also execute in "Edit with PepperSnap" window!
if (noModalOpen && (isKeyDown || (isKeyUp && vk == VK_SNAPSHOT)) && !IsModifierVk(vk)) {
UINT curMods = GetCurrentHardwareModifiers();
if (kb->flags & LLKHF_ALTDOWN) curMods |= MOD_ALT;
auto matchHk = [&](const HotkeyBinding& hk) {
return hk.vk != 0 && vk == hk.vk && curMods == hk.modifiers;
};
// Post the trigger, and note that this press was handled on its way down so the
// key-up check above cannot run it a second time.
auto postTrigger = [&](UINT trigger) -> LRESULT {
if (isKeyDown && vk == VK_SNAPSHOT) g_SnapshotHandledOnKeyDown = true;
PostMessageW(g_Daemon.hTrayWnd, trigger, 0, 0);
return 1;
};
bool overlayActive = (g_Daemon.hOverlayWnd && IsWindow(g_Daemon.hOverlayWnd));
bool canCaptureNow = !overlayActive || g_Daemon.IsEditingOpenedImage();
bool inlineEditActive = overlayActive &&
(g_Daemon.isEditingText || g_Daemon.isEditingSize || g_Daemon.isEditingRatio || g_Daemon.isEditingStroke);
if (!inlineEditActive && !g_Daemon.pinnedWindows.empty()) {
HWND hFg = GetForegroundWindow();
if (g_Daemon.IsPinnedWindow(hFg)) {
PinnedWindowData* fgData = (PinnedWindowData*)GetWindowLongPtrW(hFg, GWLP_USERDATA);
if (fgData && (fgData->isEditingSize || fgData->isEditingPct)) {
inlineEditActive = true;
}
}
}
if (matchHk(g_Daemon.hkPrevRegion)) {
return postTrigger(WM_TRIGGER_PREV_REGION);
}
if (canCaptureNow && matchHk(g_Daemon.hkRegionSnip)) {
return postTrigger(WM_TRIGGER_REGION_SNIP);
}
if (matchHk(g_Daemon.hkFullSnap)) {
return postTrigger(WM_TRIGGER_FULL_SNAP);
}
if (canCaptureNow && !inlineEditActive && matchHk(g_Daemon.hkPinClipboard)) {
return postTrigger(WM_TRIGGER_PIN_CLIPBOARD);
}
if (!inlineEditActive && matchHk(g_Daemon.hkEditClipboard)) {
return postTrigger(WM_TRIGGER_EDIT_CLIPBOARD);
}
if (!inlineEditActive && matchHk(g_Daemon.hkSearchGoogle)) {
return postTrigger(WM_TRIGGER_SEARCH_GOOGLE);
}
}
// If the Custom Area overlay is active (with no modal child window open), ensure Esc and overlay keys
// reach hOverlayWnd immediately even if a higher-band shell flyout (Notification Sidebar) held focus
if (g_Daemon.hOverlayWnd && IsWindow(g_Daemon.hOverlayWnd) && noModalOpen) {
// When "Edit with PepperSnap" is active and pinned images overlay it, let pinned windows handle
// inline size/percentage typing or pinned shortcuts when the cursor is over a pinned image
if (g_Daemon.IsEditingOpenedImage() && g_Daemon.pinnedImageAboveEdit && !g_Daemon.pinnedWindows.empty()) {
HWND hFg = GetForegroundWindow();
if (g_Daemon.IsPinnedWindow(hFg)) {
PinnedWindowData* fgData = (PinnedWindowData*)GetWindowLongPtrW(hFg, GWLP_USERDATA);
if (fgData && (fgData->isEditingSize || fgData->isEditingPct)) {
return CallNextHookEx(g_Daemon.hKeyHook, nCode, wParam, lParam);
}
}
POINT curPt = { 0, 0 };
if (GetCursorPos(&curPt)) {
HWND hAtPt = WindowFromPoint(curPt);
if (g_Daemon.IsPinnedWindow(hAtPt) && isKeyDown && !IsModifierVk(vk)) {
UINT curMods = GetCurrentHardwareModifiers();
if (kb->flags & LLKHF_ALTDOWN) curMods |= MOD_ALT;
bool ctrl = (curMods & MOD_CONTROL) != 0;
bool shift = (curMods & MOD_SHIFT) != 0;
bool alt = (curMods & MOD_ALT) != 0;
if (MatchesAnyPinnedShortcut(vk, ctrl, shift, alt)) {
if (hFg != hAtPt) {
PostMessageW(hAtPt, WM_KEYDOWN, vk, 0);
return 1;
}
return CallNextHookEx(g_Daemon.hKeyHook, nCode, wParam, lParam);
}
}
}
}
if (isKeyDown && vk == VK_ESCAPE) {
PostMessageW(g_Daemon.hOverlayWnd, WM_KEYDOWN, VK_ESCAPE, 0);
return 1;
}
if (GetForegroundWindow() != g_Daemon.hOverlayWnd) {
if (isKeyDown && !IsModifierVk(vk)) {
PostMessageW(g_Daemon.hOverlayWnd, WM_KEYDOWN, vk, 0);
return 1;
} else if (isKeyDown && (vk == VK_CONTROL || vk == VK_LCONTROL || vk == VK_RCONTROL)) {
PostMessageW(g_Daemon.hOverlayWnd, WM_KEYDOWN, VK_CONTROL, 0);
} else if (isKeyUp && (vk == VK_CONTROL || vk == VK_LCONTROL || vk == VK_RCONTROL)) {
PostMessageW(g_Daemon.hOverlayWnd, WM_KEYUP, VK_CONTROL, 0);
} else if (isKeyDown && (vk == VK_SHIFT || vk == VK_LSHIFT || vk == VK_RSHIFT)) {
PostMessageW(g_Daemon.hOverlayWnd, WM_KEYDOWN, VK_SHIFT, 0);
} else if (isKeyUp && (vk == VK_SHIFT || vk == VK_LSHIFT || vk == VK_RSHIFT)) {
PostMessageW(g_Daemon.hOverlayWnd, WM_KEYUP, VK_SHIFT, 0);
}
}
} else if ((!g_Daemon.hOverlayWnd || !IsWindow(g_Daemon.hOverlayWnd)) &&
noModalOpen &&
!g_Daemon.pinnedWindows.empty() && isKeyDown && !IsModifierVk(vk)) {
HWND hFg = GetForegroundWindow();
if (!g_Daemon.IsPinnedWindow(hFg)) {
POINT curPt = {0, 0};
if (GetCursorPos(&curPt)) {
HWND hAtPt = WindowFromPoint(curPt);
if (g_Daemon.IsPinnedWindow(hAtPt)) {
UINT curMods = GetCurrentHardwareModifiers();
if (kb->flags & LLKHF_ALTDOWN) curMods |= MOD_ALT;
bool ctrl = (curMods & MOD_CONTROL) != 0;
bool shift = (curMods & MOD_SHIFT) != 0;
bool alt = (curMods & MOD_ALT) != 0;
if (MatchesAnyPinnedShortcut(vk, ctrl, shift, alt)) {
PostMessageW(hAtPt, WM_KEYDOWN, vk, 0);
return 1;
}
}
}
}
}
}
return CallNextHookEx(g_Daemon.hKeyHook, nCode, wParam, lParam);
}
DWORD g_InputHookThreadId = 0;
DWORD WINAPI InputHookWorkerThread(LPVOID) {
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
g_Daemon.hKeyHook = SetWindowsHookExW(WH_KEYBOARD_LL, LowLevelKeyboardProc, g_Daemon.hInst, 0);
// 15ms hardware key-state poller + debounced hook health check
UINT_PTR pollTimer = SetTimer(nullptr, 0, 15, nullptr);
HWND lastFgWnd = GetForegroundWindow();
DWORD lastHookInstallTick = GetTickCount();
DWORD pendingFgRehookTick = 0;
bool wasDownPrev = false;
bool wasDownReg = false;
bool wasDownFull = false;
bool wasDownPin = false;
bool wasDownEdit = false;
bool wasDownSearch = false;
MSG msg;
while (GetMessageW(&msg, nullptr, 0, 0) > 0) {
if (msg.message == WM_TIMER && msg.wParam == pollTimer) {
DWORD now = GetTickCount();
HWND curFg = GetForegroundWindow();
if (curFg && curFg != lastFgWnd &&
curFg != g_Daemon.hOverlayWnd && curFg != g_Daemon.hOptionsWnd &&
!g_Daemon.IsPinnedWindow(curFg)) {
lastFgWnd = curFg;
pendingFgRehookTick = (now == 0) ? 1 : now;
}
// Reinstall WH_KEYBOARD_LL only if the hook handle is invalid, or after a >=250ms debounce following a foreground change
if (!g_Daemon.hKeyHook) {
g_Daemon.hKeyHook = SetWindowsHookExW(WH_KEYBOARD_LL, LowLevelKeyboardProc, g_Daemon.hInst, 0);
lastHookInstallTick = now;
pendingFgRehookTick = 0;
} else if (pendingFgRehookTick != 0 &&
(now - pendingFgRehookTick >= 250) &&
(now - lastHookInstallTick >= 250)) {
HHOOK hNewHook = SetWindowsHookExW(WH_KEYBOARD_LL, LowLevelKeyboardProc, g_Daemon.hInst, 0);
if (hNewHook) {
HHOOK hOldHook = g_Daemon.hKeyHook;
g_Daemon.hKeyHook = hNewHook;
if (hOldHook) UnhookWindowsHookEx(hOldHook);
}
lastHookInstallTick = now;
pendingFgRehookTick = 0;
}
// Direct GetAsyncKeyState polling fallback for borderless/fullscreen games that swallow hooks
if (g_RecordingHotkeySlot == 0 && g_Daemon.hTrayWnd) {
UINT curMods = GetCurrentHardwareModifiers();
auto isHkPressed = [&](const HotkeyBinding& hk) -> bool {
if (hk.vk == 0 || curMods != hk.modifiers) return false;
SHORT st = GetAsyncKeyState((int)hk.vk);
if (hk.vk == VK_SNAPSHOT) {
return (st & 0x8001) != 0;
}
return (st & 0x8000) != 0;
};
bool downPrev = isHkPressed(g_Daemon.hkPrevRegion);
bool downReg = isHkPressed(g_Daemon.hkRegionSnip);
bool downFull = isHkPressed(g_Daemon.hkFullSnap);
bool downPin = isHkPressed(g_Daemon.hkPinClipboard);
bool downEdit = isHkPressed(g_Daemon.hkEditClipboard);
bool downSearch = isHkPressed(g_Daemon.hkSearchGoogle);
if (downPrev && !wasDownPrev) {
PostMessageW(g_Daemon.hTrayWnd, WM_TRIGGER_PREV_REGION, 0, 0);
} else if (downReg && !wasDownReg) {
PostMessageW(g_Daemon.hTrayWnd, WM_TRIGGER_REGION_SNIP, 0, 0);
} else if (downFull && !wasDownFull) {
PostMessageW(g_Daemon.hTrayWnd, WM_TRIGGER_FULL_SNAP, 0, 0);
} else if (downPin && !wasDownPin) {
PostMessageW(g_Daemon.hTrayWnd, WM_TRIGGER_PIN_CLIPBOARD, 0, 0);
} else if (downEdit && !wasDownEdit) {
PostMessageW(g_Daemon.hTrayWnd, WM_TRIGGER_EDIT_CLIPBOARD, 0, 0);
} else if (downSearch && !wasDownSearch) {
PostMessageW(g_Daemon.hTrayWnd, WM_TRIGGER_SEARCH_GOOGLE, 0, 0);
}
wasDownPrev = downPrev;
wasDownReg = downReg;
wasDownFull = downFull;
wasDownPin = downPin;
wasDownEdit = downEdit;
wasDownSearch = downSearch;
}
continue;
}
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
KillTimer(nullptr, pollTimer);
if (g_Daemon.hKeyHook) {
UnhookWindowsHookEx(g_Daemon.hKeyHook);
g_Daemon.hKeyHook = nullptr;
}
return 0;
}