-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboardListener.cpp
More file actions
137 lines (115 loc) · 3.9 KB
/
KeyboardListener.cpp
File metadata and controls
137 lines (115 loc) · 3.9 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
#include <iostream>
#include <windows.h>
#include <map>
#include <deque>
// New inclusions
#include <cstdint>
#include <array>
#include "KeystrokeEvent.h"
#include "EventQueue.h"
#include "Batcher.h"
// Old compilation command saved for reference
// Compilation: x86_64-w64-mingw32-g++ KeyboardListener.cpp -o KeyboardHookTest.exe -static -luser32 -lgdi32 (need to compile with "static" because of lack of DLL's)
// Global handle for the hook
static HHOOK g_hook = nullptr;
static SpscRing<KeystrokeEvent, 1024> g_q;
// Batcher worker (drains g_q -> JSON -> named pipe)
static Batcher g_batcher(g_q);
// Timestamping
static inline uint64_t now_micros() {
static LARGE_INTEGER freq = []{ LARGE_INTEGER f; QueryPerformanceFrequency(&f); return f; }();
LARGE_INTEGER c; QueryPerformanceCounter(&c);
return static_cast<uint64_t>((c.QuadPart * 1'000'000) / freq.QuadPart);
}
// Modifier snapshot (Ctrl, Alt, Shift, Win)
static inline std::array<bool,4> snapshot_mods() {
auto down = [](int vk){ return (GetKeyState(vk) & 0x8000) != 0; };
return std::array<bool,4>{
down(VK_CONTROL), // Ctrl
down(VK_MENU), // Alt
down(VK_SHIFT), // Shift
(down(VK_LWIN) || down(VK_RWIN)) // Win
};
}
// Translate vk to printable character
static inline char32_t vk_to_char(UINT vk, UINT sc) {
BYTE keystate[256];
if (!GetKeyboardState(keystate)) return 0;
WCHAR buf[4] = {0};
HKL layout = GetKeyboardLayout(0);
// Note: ToUnicodeEx can have side-effects for dead keys, this is "good enough" for an overlay.
int rc = ToUnicodeEx(vk, sc, keystate, buf, 4, 0, layout);
if (rc <= 0) return 0;
if (!iswprint(buf[0])) return 0;
return static_cast<char32_t>(buf[0]);
}
// Push helpers
static inline void emit_down(UINT vk, UINT sc) {
KeystrokeEvent e{};
e.type = KeystrokeEvent::Type::Down;
e.vk = static_cast<uint16_t>(vk);
e.ch = 0;
e.mods = snapshot_mods();
e.ts_micros = now_micros();
g_q.try_push(e);
// Also emit a Char if there is a printable character for this Down
if (char32_t ch = vk_to_char(vk, sc)) {
KeystrokeEvent c = e;
c.type = KeystrokeEvent::Type::Char;
c.ch = ch;
g_q.try_push(c);
}
}
static inline void emit_up(UINT vk) {
KeystrokeEvent e{};
e.type = KeystrokeEvent::Type::Up;
e.vk = static_cast<uint16_t>(vk);
e.ch = 0;
e.mods = snapshot_mods();
e.ts_micros = now_micros();
g_q.try_push(e);
}
// LL keyboard hook callback
static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
const KBDLLHOOKSTRUCT* p = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);
switch (wParam) {
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
emit_down(p->vkCode, p->scanCode);
break;
case WM_KEYUP:
case WM_SYSKEYUP:
emit_up(p->vkCode);
break;
}
}
return CallNextHookEx(nullptr, nCode, wParam, lParam);
}
// Install/uninstall hook
static void SetGlobalHook() {
g_hook = SetWindowsHookExW(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandleW(nullptr), 0);
}
static void UnsetGlobalHook() {
if (g_hook) { UnhookWindowsHookEx(g_hook); g_hook = nullptr; }
}
// Main
int main() {
// Start the batcher FIRST so frames have somewhere to go
g_batcher.Start();
SetGlobalHook();
if (!g_hook) {
// If hook fails, stop batcher and exit
g_batcher.Stop();
return 1;
}
// Required message loop for WH_KEYBOARD_LL owner thread
MSG msg;
while (GetMessageW(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
UnsetGlobalHook();
g_batcher.Stop();
return 0;
}