-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.c
366 lines (312 loc) · 13 KB
/
input.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
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
#define WIN32_NO_STATUS
#include <windows.h>
#include <winternl.h>
#include <stdio.h>
#include <stdlib.h>
#include <search.h>
#include <ctype.h>
#undef WIN32_NO_STATUS
#include <ntstatus.h>
#include "winutil.h"
#include "input.h"
#pragma comment(lib, "USER32")
#define MAX_KEY_LEN 16
static INIT_ONCE KeyTablesInit = INIT_ONCE_STATIC_INIT;
static CHAR RegKeyNames[UCHAR_MAX][MAX_KEY_LEN];
static CHAR ExtKeyNames[UCHAR_MAX][MAX_KEY_LEN];
static BOOL CALLBACK InitializeKeyTables(PINIT_ONCE InitOnce,
PVOID Parameter,
PVOID *Context)
{
for (DWORD Key = 0; Key < UCHAR_MAX; Key++) {
GetKeyNameText((0 << 24) | (Key << 16), RegKeyNames[Key], MAX_KEY_LEN);
GetKeyNameText((1 << 24) | (Key << 16), ExtKeyNames[Key], MAX_KEY_LEN);
}
return TRUE;
}
// I think you cannot possibly encode more than 9 keys into a key event, thats
// 8 modifiers and at most 1 non-modifier
// e.g.
// Caps Lock+Left Alt+Right Alt+Left Ctrl+Right Ctrl+Num Lock+Scroll Lock+Shift+X
#define MAX_KEY_COMBINATION 9
// Takes a PKEY_EVENT_RECORD, and translates it into a string.
BOOL EncodeKeyString(PKEY_EVENT_RECORD Record, PCHAR HotKey, SIZE_T MaxLen)
{
WORD ScanCode = Record->wVirtualScanCode;
WORD KeyCode = Record->wVirtualKeyCode;
BOOL Enhanced = Record->dwControlKeyState & ENHANCED_KEY;
DWORD NumKeys = 0;
PCHAR KeyNames[MAX_KEY_COMBINATION] = {0};
InitOnceExecuteOnce(&KeyTablesInit, InitializeKeyTables, NULL, NULL);
ZeroMemory(HotKey, MaxLen);
// These should be in order, for example it would be weird to say
// "Alt+Ctrl+Delete", everyone says "Ctrl+Alt+delete". Therefore, Ctrl must
// be checked first. I think the natural order is:
// Ctrl, Alt, Shift, {Num, Scroll, Caps}, Key
//
// It doesn't really matter what order you press the modifiers in.
//
// The exception is if you're *just* pressing modifiers (e.g. Ctrl+Alt)
// Then the *last* key you pressed is printed last, because that's what the
// scancode is and it makes a difference.
if (Record->dwControlKeyState & LEFT_CTRL_PRESSED) {
if (Record->dwControlKeyState & RIGHT_ALT_PRESSED) {
// There is no difference between Ctrl+Right Alt and Right Alt
// Don't print anything.
} else if (KeyCode != VK_CONTROL && Enhanced) {
KeyNames[NumKeys++] = RegKeyNames[MapVirtualKey(VK_CONTROL, MAPVK_VK_TO_VSC)];
} else if (KeyCode != VK_CONTROL && !Enhanced) {
KeyNames[NumKeys++] = RegKeyNames[MapVirtualKey(VK_CONTROL, MAPVK_VK_TO_VSC)];
}
}
if (Record->dwControlKeyState & RIGHT_CTRL_PRESSED) {
if (KeyCode != VK_CONTROL && !Enhanced) {
KeyNames[NumKeys++] = RegKeyNames[MapVirtualKey(VK_RCONTROL, MAPVK_VK_TO_VSC)];
}
}
if (Record->dwControlKeyState & LEFT_ALT_PRESSED) {
if (KeyCode != VK_MENU) {
KeyNames[NumKeys++] = RegKeyNames[MapVirtualKey(VK_LMENU, MAPVK_VK_TO_VSC)];
} else if (Enhanced && (Record->dwControlKeyState & RIGHT_ALT_PRESSED)) {
// This must be Alt+Right Alt
KeyNames[NumKeys++] = RegKeyNames[MapVirtualKey(VK_LMENU, MAPVK_VK_TO_VSC)];
}
}
if (Record->dwControlKeyState & RIGHT_ALT_PRESSED) {
if (KeyCode != VK_MENU) {
KeyNames[NumKeys++] = RegKeyNames[MapVirtualKey(VK_RMENU, MAPVK_VK_TO_VSC)];
} else if (!Enhanced && (Record->dwControlKeyState & LEFT_ALT_PRESSED)) {
// This must be Right Alt+Alt
KeyNames[NumKeys++] = ExtKeyNames[MapVirtualKey(VK_RMENU, MAPVK_VK_TO_VSC)];
}
}
// You can tell the difference between
// LShift and RShift in scancodes, but not as modifiers
//
// i.e. LShift+X, RShift+X, LShift+RShift+X are all the same.
//
if (Record->dwControlKeyState & SHIFT_PRESSED) {
if (KeyCode != VK_SHIFT) {
KeyNames[NumKeys++] = RegKeyNames[MapVirtualKey(VK_SHIFT, MAPVK_VK_TO_VSC)];
}
}
if (Record->dwControlKeyState & NUMLOCK_ON) {
if (KeyCode != VK_NUMLOCK) {
KeyNames[NumKeys++] = RegKeyNames[MapVirtualKey(VK_NUMLOCK, MAPVK_VK_TO_VSC)];
}
}
if (Record->dwControlKeyState & SCROLLLOCK_ON) {
if (KeyCode != VK_SCROLL) {
KeyNames[NumKeys++] = RegKeyNames[MapVirtualKey(VK_SCROLL, MAPVK_VK_TO_VSC)];
}
}
if (Record->dwControlKeyState & CAPSLOCK_ON) {
if (KeyCode != VK_CAPITAL) {
KeyNames[NumKeys++] = RegKeyNames[MapVirtualKey(VK_CAPITAL, MAPVK_VK_TO_VSC)];
}
}
if (Record->dwControlKeyState & ENHANCED_KEY) {
KeyNames[NumKeys++] = ExtKeyNames[Record->wVirtualScanCode];
} else {
KeyNames[NumKeys++] = RegKeyNames[Record->wVirtualScanCode];
}
success:
for (DWORD Key = 0; Key < NumKeys; Key++) {
// A Key couldn't be decoded.
if (KeyNames[Key] == NULL)
return FALSE;
strncat_s(HotKey, MaxLen, KeyNames[Key], _TRUNCATE);
// Append a + if not already there.
if (Key != NumKeys - 1) {
strncat_s(HotKey, MaxLen, "+", _TRUNCATE);
}
}
return NumKeys > 0;
}
// Decodes a string of the form "Ctrl+Shift+A" into a PKEY_EVENT_RECORD
BOOL DecodeKeyString(LPCSTR HotKey, PKEY_EVENT_RECORD Record)
{
PCHAR KeyCombination = strdupa(HotKey);
PCHAR CurKey;
DWORD CtrlState = 0;
ZeroMemory(Record, sizeof *Record);
InitOnceExecuteOnce(&KeyTablesInit, InitializeKeyTables, NULL, NULL);
for (CurKey = strtok(KeyCombination, "+");
CurKey;
CurKey = strtok(NULL, "+")) {
DWORD NumElems = UCHAR_MAX;
WORD ScanCode;
UINT KeyCode;
PCHAR Result;
Result = _lfind(CurKey, RegKeyNames, &NumElems, MAX_KEY_LEN, stricmp);
ScanCode = (DWORD)(Result - (PCHAR) RegKeyNames) / MAX_KEY_LEN;
if (!Result) {
Result = _lfind(CurKey, ExtKeyNames, &NumElems, MAX_KEY_LEN, stricmp);
ScanCode = (DWORD)(Result - (PCHAR) ExtKeyNames) / MAX_KEY_LEN;
CtrlState |= ENHANCED_KEY;
}
// Failed to decode keyname.
if (!Result) {
return FALSE;
}
KeyCode = MapVirtualKey(ScanCode, MAPVK_VSC_TO_VK);
switch (KeyCode) {
case VK_CAPITAL:
CtrlState |= CAPSLOCK_ON;
break;
case VK_MENU:
case VK_LMENU:
if (CtrlState & ENHANCED_KEY) {
CtrlState |= RIGHT_ALT_PRESSED;
// It's not possible to set RIGHT_ALT_PRESSED without
// LEFT_CTRL_PRESSED
CtrlState |= LEFT_CTRL_PRESSED;
} else {
CtrlState |= LEFT_ALT_PRESSED;
}
break;
case VK_RMENU:
CtrlState |= RIGHT_ALT_PRESSED;
CtrlState |= LEFT_CTRL_PRESSED;
break;
case VK_CONTROL:
if (CtrlState & ENHANCED_KEY) {
CtrlState |= RIGHT_CTRL_PRESSED;
} else {
CtrlState |= LEFT_CTRL_PRESSED;
}
break;
case VK_RCONTROL:
CtrlState |= RIGHT_CTRL_PRESSED;
break;
case VK_NUMLOCK:
CtrlState |= NUMLOCK_ON;
break;
case VK_SCROLL:
CtrlState |= SCROLLLOCK_ON;
break;
case VK_SHIFT:
case VK_LSHIFT:
case VK_RSHIFT:
CtrlState |= SHIFT_PRESSED;
break;
}
// We've decoded the modifiers, but what if user just wants Ctrl+Alt?
// Whatver the last key is, that is the scancode.
Record->bKeyDown = TRUE;
Record->wRepeatCount = 1;
Record->wVirtualScanCode = ScanCode;
Record->wVirtualKeyCode = KeyCode;
Record->uChar.AsciiChar = MapVirtualKey(KeyCode, MAPVK_VK_TO_CHAR);
Record->dwControlKeyState = CtrlState;
// I dunno what the rules are for setting this field, I've tried
// to guess them from observation.
if (CtrlState & LEFT_CTRL_PRESSED) {
if (CtrlState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
Record->uChar.AsciiChar = 0;
}
}
if (KeyCode == Record->uChar.AsciiChar && isalpha(KeyCode)) {
// In general, for ascii chars the lowercase form seems to be used.
Record->uChar.AsciiChar = tolower(KeyCode);
// If you hold ctrl, you get the ctrl characters, in alphabetic
// order.
if (CtrlState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
// Is this right??????????????
Record->uChar.AsciiChar -= 'a' - 1;
}
}
if (KeyCode == Record->uChar.AsciiChar && isdigit(KeyCode)) {
if (CtrlState & SHIFT_PRESSED) {
// I don't think there's any way to figure out what this should be
// without localization, so I'll just simulate it.
Record->uChar.AsciiChar = ")!@#$%^&*("[KeyCode - '0'];
}
// fallthrough intentional.
// If CTRL is pressed with a digit, AsciiChar is always 0.
if (CtrlState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
Record->uChar.AsciiChar = 0;
}
}
// https://www.codeproject.com/Questions/511401/apluslittleplusaboutplusMSGplusHandingplusaboutplu
if (KeyCode == VK_BACK) {
if (CtrlState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
if ((CtrlState & ~(LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) == 0) {
Record->uChar.AsciiChar = 0x7f;
}
}
}
// This needs some work to understand.
if (CtrlState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
switch (KeyCode) {
case VK_RETURN:
Record->uChar.AsciiChar = '\n';
break;
case VK_OEM_7: // '#'
Record->uChar.AsciiChar = 28;
break;
case VK_OEM_4: // '['
case VK_OEM_6: // ']'
case VK_OEM_5: // '\\'
Record->uChar.AsciiChar -= 'A' - 1;
break;
case VK_OEM_1: // ';'
case VK_OEM_3: // '~'
case VK_OEM_PERIOD:
case VK_OEM_MINUS:
case VK_OEM_PLUS:
case VK_TAB:
Record->uChar.AsciiChar = 0;
break;
}
}
}
return TRUE;
}
VOID DumpKeyCodes()
{
InitOnceExecuteOnce(&KeyTablesInit, InitializeKeyTables, NULL, NULL);
for (DWORD Key = 0; Key < UCHAR_MAX; Key++) {
if (strlen(RegKeyNames[Key])) printf("R %02X %s\n", Key, RegKeyNames[Key]);
if (strlen(ExtKeyNames[Key])) printf("E %02X %s\n", Key, ExtKeyNames[Key]);
}
}
VOID PrintKeyEvent(PKEY_EVENT_RECORD Key)
{
UINT MapScanCode = MapVirtualKey(Key->wVirtualKeyCode, MAPVK_VK_TO_VSC);
UINT MapChar = MapVirtualKey(Key->wVirtualKeyCode, MAPVK_VK_TO_CHAR);
UINT MapKeyCode = MapVirtualKey(Key->wVirtualScanCode, MAPVK_VSC_TO_VK);
UINT MapExKeyCode = MapVirtualKey(Key->wVirtualScanCode, MAPVK_VSC_TO_VK_EX);
printf("Key event: \n");
printf(" bKeyDown: %u\n", Key->bKeyDown);
printf(" wRepeatCount: %u\n", Key->wRepeatCount);
printf(" wVirtualKeyCode: %#02x\n", Key->wVirtualKeyCode);
printf(" MAPVK_VK_TO_VSC => %#02x\n", MapScanCode);
printf(" MAPVK_VK_TO_CHAR => %#02x\n", MapChar);
printf(" wVirtualScanCode: %#02x\n", Key->wVirtualScanCode);
printf(" MAPVK_VSC_TO_VK => %#02x\n", MapKeyCode);
printf(" MAPVK_VSC_TO_VK_EX => %#02x\n", MapExKeyCode);
printf(" uChar: \n");
printf(" UnicodeChar: '%C' (%#04x)\n", Key->uChar.UnicodeChar, Key->uChar.UnicodeChar);
printf(" AsciiChar: '%c' (%#02x)\n", Key->uChar.AsciiChar, Key->uChar.AsciiChar);
printf(" VkKeyScanA => %#02x\n", VkKeyScanA(Key->uChar.AsciiChar));
printf(" dwControlKeyState: %u\n", Key->dwControlKeyState);
if (Key->dwControlKeyState & CAPSLOCK_ON)
printf("CAPSLOCK_ON\n");
if (Key->dwControlKeyState & ENHANCED_KEY)
printf("ENHANCED_KEY\n");
if (Key->dwControlKeyState & LEFT_ALT_PRESSED)
printf("LEFT_ALT_PRESSED\n");
if (Key->dwControlKeyState & LEFT_CTRL_PRESSED)
printf("LEFT_CTRL_PRESSED\n");
if (Key->dwControlKeyState & NUMLOCK_ON)
printf("NUMLOCK_ON\n");
if (Key->dwControlKeyState & RIGHT_ALT_PRESSED)
printf("RIGHT_ALT_PRESSED\n");
if (Key->dwControlKeyState & RIGHT_CTRL_PRESSED)
printf("RIGHT_CTRL_PRESSED\n");
if (Key->dwControlKeyState & SCROLLLOCK_ON)
printf("SCROLLLOCK_ON\n");
if (Key->dwControlKeyState & SHIFT_PRESSED)
printf("SHIFT_PRESSED\n");
}