-
Notifications
You must be signed in to change notification settings - Fork 1
/
hook.c
208 lines (191 loc) · 4.79 KB
/
hook.c
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
#include <windows.h>
#include <ctype.h>
#include <stdlib.h>
#include "common.h"
/* A Node is either a branch (.type = 'B') or leaf (.type = 'L') */
typedef struct Node_ {
char type;
union {
long rune;
struct {
int n;
struct Step_ *list;
} branch;
} u;
} Node;
/* A Step represents a link to another Node. */
typedef struct Step_ {
char ch;
struct Node_ next;
} Step;
#define BRANCH(num, lst...) {.type = 'B', .u.branch = {.n = num, .list = (Step[])lst}}
#define STEM(chr, node) {.ch = chr, .next = node}
#define LEAF(r) {.type = 'L', .u.rune = r}
Node root =
#include "kbd.h"
;
/* The maximum number of characters we're willing to "hold", or buffer, before giving up. */
#define MAXHOLD 16
typedef struct HeldChar_ {
char ch;
DWORD time;
} HeldChar;
struct {
/* Mode can be '\0' for "waiting", 'T' for "traversing nodes" or 'X' for "heXadecimal input" */
char mode;
union {
Node *node;
struct {
short count;
char digit[5]; /* digit[4] serves as a NUL terminator; it's never written */
} hex;
} u;
int nheld;
HeldChar held[MAXHOLD];
} state = {0};
HHOOK nxtHook = 0;
// GetKeyboardState doesn't seem to work, maybe because we never receive focus?
// Managing the shift state manually allows ToAscii to work.
BYTE kbd[256] = {0};
Node*
node_walk(Node *node, char c)
{
if (node->type == 'B') {
int i;
for (i = 0; i < node->u.branch.n; i++) {
// TODO binary search
if (node->u.branch.list[i].ch == c) {
return &node->u.branch.list[i].next;
}
}
}
return NULL;
}
/* Adds a character to the held list. Returns 0 if it won't fit. */
int
hold(KBDLLHOOKSTRUCT *khook, char ch)
{
if (state.nheld >= MAXHOLD) {
return 0;
}
state.held[state.nheld++] = (HeldChar){ch, khook->time};
return 1;
}
char
toChar(KBDLLHOOKSTRUCT *khook)
{
/* Apparently calling ToAscii on a deadkey in a low-level hook interferes with normal operation... */
int isdeadkey = (MapVirtualKey(khook->vkCode, MAPVK_VK_TO_CHAR) & 0x80000000);
if (!isdeadkey) {
WORD chr;
if (ToAscii(khook->vkCode, khook->scanCode, kbd, &chr, 0) == 1) {
return chr;
}
}
return '\0';
}
void
sendrune(long rune)
{
INPUT in = {.type = INPUT_KEYBOARD};
in.ki.wScan = rune;
in.ki.dwFlags = KEYEVENTF_UNICODE;
SendInput(1, &in, sizeof(INPUT));
}
LRESULT CALLBACK
KeyHook(int nCode, WPARAM wParam, LPARAM lParam)
{
char chr;
int isDown = (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN);
KBDLLHOOKSTRUCT *khook = (KBDLLHOOKSTRUCT*)lParam;
if (khook->vkCode == VK_LSHIFT || khook->vkCode == VK_RSHIFT) {
kbd[khook->vkCode] = isDown ? 0x80 : 0x00;
kbd[VK_SHIFT] = kbd[VK_LSHIFT] | kbd[VK_RSHIFT];
return CallNextHookEx(nxtHook, nCode, wParam, lParam);
}
if (nCode < 0 || !isDown || khook->flags & LLKHF_INJECTED) {
/* ignore key-up/injected events */
return CallNextHookEx(nxtHook, nCode, wParam, lParam);
}
switch (state.mode) {
case '\0':
if (khook->vkCode == VK_RMENU) {
state.mode = 'T';
state.u.node = &root;
state.nheld = 0;
return -1;
}
break;
case 'T':
if (chr = toChar(khook)) {
Node *next = node_walk(state.u.node, chr);
if (next) {
if (next->type == 'L') {
if (next->u.rune == RESERVED_HEX_ESCAPE) {
state.mode = 'X';
state.u.hex.count = 0;
if (hold(khook, chr)) {
return -1;
}
} else {
sendrune(next->u.rune);
state.mode = '\0';
return -1;
}
} else if (next->type == 'B') {
state.u.node = next;
if (hold(khook, chr)) {
return -1;
}
}
}
}
break;
case 'X':
if (chr = toChar(khook)) {
if (isxdigit(chr)) {
state.u.hex.digit[state.u.hex.count++] = chr;
if (state.u.hex.count == 4) {
sendrune(strtol(state.u.hex.digit, NULL, 16));
state.mode = '\0';
return -1;
} else if (hold(khook, chr)) {
return -1;
}
}
}
break;
}
if (state.mode != '\0') {
/* invalid input; revert to pass-through state */
state.mode = '\0';
if (khook->vkCode == VK_ESCAPE) {
return -1; /* avoid replaying pending keypresses if ESC used to explicitly cancel */
}
INPUT in[2*MAXHOLD] = {0};
int i;
for (i = 0; i < state.nheld; i++) {
in[2*i].type = INPUT_KEYBOARD;
in[2*i].ki.wScan = state.held[i].ch;
in[2*i].ki.dwFlags = KEYEVENTF_UNICODE;
in[2*i].ki.time = state.held[i].time;
/* Need to synthesise a key-up to correctly replay repeated characters */
in[2*i + 1] = in[2*i];
in[2*i + 1].ki.dwFlags |= KEYEVENTF_KEYUP;
}
SendInput(state.nheld*2, in, sizeof(INPUT));
state.nheld = 0;
}
return CallNextHookEx(nxtHook, nCode, wParam, lParam);
}
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
nxtHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyHook, GetModuleHandle(NULL), 0);
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}