-
Notifications
You must be signed in to change notification settings - Fork 5
/
kbd_ext.c
538 lines (417 loc) · 12.2 KB
/
kbd_ext.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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/**
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/
#include <stdio.h>
#include <tchar.h>
#include "kbd_ext.h"
#include "vkcodes.h"
typedef PKBDTABLES(CALLBACK* KbdLayerDescriptor)(VOID);
PVK_TO_WCHARS1 pVkToWchars1 = NULL;
PVK_TO_WCHARS2 pVkToWchars2 = NULL;
PVK_TO_WCHARS3 pVkToWchars3 = NULL;
PVK_TO_WCHARS4 pVkToWchars4 = NULL;
PVK_TO_WCHARS5 pVkToWchars5 = NULL;
PVK_TO_WCHARS6 pVkToWchars6 = NULL;
PVK_TO_WCHARS7 pVkToWchars7 = NULL;
PVK_TO_WCHARS8 pVkToWchars8 = NULL;
PVK_TO_WCHARS9 pVkToWchars9 = NULL;
PVK_TO_WCHARS10 pVkToWchars10 = NULL;
PMODIFIERS pgCharModifiers;
PDEADKEY pgDeadKey;
HINSTANCE loadKeyboardLayout()
{
int i;
PKBDTABLES pKbd;
HINSTANCE kbdLibrary;
TCHAR layoutFile[MAX_PATH];
TCHAR systemDirectory[MAX_PATH];
TCHAR kbdLayoutFilePath[MAX_PATH];
KbdLayerDescriptor pKbdLayerDescriptor = NULL;
if (getKeyboardLayoutFile(layoutFile, sizeof(layoutFile)) == -1)
return NULL;
#ifdef BUILD_WOW6432
_stprintf_s(systemDirectory, MAX_PATH, _T("%s"), "C:\\Windows\\SysWOW64");
#else
GetSystemDirectory(systemDirectory, MAX_PATH);
#endif
_stprintf_s(kbdLayoutFilePath, MAX_PATH, _T("%s\\%s"), systemDirectory, layoutFile);
//printf("keyboard layout path: %s\n", kbdLayoutFilePath);
kbdLibrary = LoadLibrary(kbdLayoutFilePath);
pKbdLayerDescriptor = (KbdLayerDescriptor) GetProcAddress(kbdLibrary, "KbdLayerDescriptor");
if (!pKbdLayerDescriptor)
return NULL;
pKbd = pKbdLayerDescriptor();
if (!pKbd)
return NULL;
i = 0;
do
{
INIT_PVK_TO_WCHARS(i, 1)
INIT_PVK_TO_WCHARS(i, 2)
INIT_PVK_TO_WCHARS(i, 3)
INIT_PVK_TO_WCHARS(i, 4)
INIT_PVK_TO_WCHARS(i, 5)
INIT_PVK_TO_WCHARS(i, 6)
INIT_PVK_TO_WCHARS(i, 7)
INIT_PVK_TO_WCHARS(i, 8)
INIT_PVK_TO_WCHARS(i, 9)
INIT_PVK_TO_WCHARS(i, 10)
i++;
}
while (pKbd->pVkToWcharTable[i].cbSize != 0);
pgCharModifiers = pKbd->pCharModifiers;
pgDeadKey = pKbd->pDeadKey;
return kbdLibrary;
}
int unloadKeyboardLayout(HINSTANCE kbdLibrary)
{
if (kbdLibrary)
FreeLibrary(kbdLibrary);
return 0;
}
int convertVirtualKeyToWChar(int virtualKey, PWCHAR outputChar, PWCHAR deadChar)
{
int i = 0;
short state = 0;
int capsLock;
int shift = -1;
int mod = 0;
int charCount = 0;
WCHAR baseChar;
WCHAR diacritic;
*outputChar = 0;
capsLock = (GetKeyState(VK_CAPITAL) & 0x1);
do
{
state = GetAsyncKeyState(pgCharModifiers->pVkToBit[i].Vk);
if (pgCharModifiers->pVkToBit[i].Vk == VK_SHIFT)
shift = i + 1; // Get modification number for Shift key
if (state & ~SHRT_MAX)
{
if (mod == 0)
mod = i + 1;
else
mod = 0; // Two modifiers at the same time!
}
i++;
}
while (pgCharModifiers->pVkToBit[i].Vk != 0);
SEARCH_VK_IN_CONVERSION_TABLE(1)
SEARCH_VK_IN_CONVERSION_TABLE(2)
SEARCH_VK_IN_CONVERSION_TABLE(3)
SEARCH_VK_IN_CONVERSION_TABLE(4)
SEARCH_VK_IN_CONVERSION_TABLE(5)
SEARCH_VK_IN_CONVERSION_TABLE(6)
SEARCH_VK_IN_CONVERSION_TABLE(7)
SEARCH_VK_IN_CONVERSION_TABLE(8)
SEARCH_VK_IN_CONVERSION_TABLE(9)
SEARCH_VK_IN_CONVERSION_TABLE(10)
if (*deadChar != 0) // I see dead characters...
{
i = 0;
do
{
baseChar = (WCHAR) pgDeadKey[i].dwBoth;
diacritic = (WCHAR) (pgDeadKey[i].dwBoth >> 16);
if ((baseChar == *outputChar) && (diacritic == *deadChar))
{
*deadChar = 0;
*outputChar = (WCHAR) pgDeadKey[i].wchComposed;
}
i++;
}
while (pgDeadKey[i].dwBoth != 0);
}
return charCount;
}
int ListKeyboardLayouts()
{
int status;
HKL list[256];
/**
* GetKeyboardLayoutList() lists only the keyboard layouts
* available in the keyboard layout switcher, as opposed to
* all possible keyboard layouts.
*/
status = GetKeyboardLayoutList(sizeof(list) / sizeof(HKL), (HKL*) list);
//printf("ListKeyboardLayouts: %d\n", status);
return 0;
}
KbdLayerDescriptor LoadKbdLayerDescriptor(HINSTANCE* kbdLibrary, TCHAR* layoutFile)
{
TCHAR systemDirectory[MAX_PATH];
TCHAR kbdLayoutFilePath[MAX_PATH];
KbdLayerDescriptor pKbdLayerDescriptor = NULL;
*kbdLibrary = NULL;
#ifdef BUILD_WOW6432
_stprintf_s(systemDirectory, MAX_PATH, _T("%s"), "C:\\Windows\\SysWOW64");
#else
GetSystemDirectory(systemDirectory, MAX_PATH);
#endif
_stprintf_s(kbdLayoutFilePath, MAX_PATH, _T("%s\\%s"), systemDirectory, layoutFile);
printf("Loading %s\n", kbdLayoutFilePath);
*kbdLibrary = LoadLibrary(kbdLayoutFilePath);
pKbdLayerDescriptor = (KbdLayerDescriptor) GetProcAddress(*kbdLibrary, "KbdLayerDescriptor");
if (!pKbdLayerDescriptor)
return NULL;
return pKbdLayerDescriptor;
}
void PrintVirtualKeyFlags(USHORT flags)
{
int count = 0;
if (flags & KBDEXT)
{
if (count)
printf(" | ");
printf("KBDEXT");
count++;
}
if (flags & KBDMULTIVK)
{
if (count)
printf(" | ");
printf("KBDMULTIVK");
count++;
}
if (flags & KBDSPECIAL)
{
if (count)
printf(" | ");
printf("KBDSPECIAL");
count++;
}
if (flags & KBDNUMPAD)
{
if (count)
printf(" | ");
printf("KBDNUMPAD");
count++;
}
if (flags & KBDUNICODE)
{
if (count)
printf(" | ");
printf("KBDUNICODE");
count++;
}
if (flags & KBDINJECTEDVK)
{
if (count)
printf(" | ");
printf("KBDINJECTEDVK");
count++;
}
if (flags & KBDMAPPEDVK)
{
if (count)
printf(" | ");
printf("KBDMAPPEDVK");
count++;
}
if (flags & KBDBREAK)
{
if (count)
printf(" | ");
printf("KBDBREAK");
count++;
}
}
int PrintKeyboardLayout(TCHAR* kbdName)
{
HKEY hKey;
int i, j, k;
DWORD dwSize;
PKBDTABLES pKbd;
BYTE* ModNumber;
PDEADKEY pDeadKey;
HINSTANCE kbdLibrary;
DWORD varType = REG_SZ;
TCHAR kbdKeyPath[MAX_PATH];
TCHAR kbdLayoutFile[64];
TCHAR kbdLayoutText[256];
PVK_TO_WCHARS pVkToWchars;
PVSC_VK pVSCtoVK_E0;
PVSC_VK pVSCtoVK_E1;
PVK_TO_BIT pVkToBit;
USHORT *KBD_LONG_POINTER pusVSCtoVK;
KbdLayerDescriptor pKbdLayerDescriptor;
_stprintf_s(kbdKeyPath, sizeof(kbdKeyPath) / sizeof(TCHAR), _T("SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts\\%s"), kbdName);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR) kbdKeyPath, 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
return -1;
dwSize = 64;
if (RegQueryValueEx(hKey, _T("Layout File"), NULL, &varType, (BYTE*) kbdLayoutFile, &dwSize) != ERROR_SUCCESS)
return -1;
dwSize = 256;
if (RegQueryValueEx(hKey, _T("Layout Text"), NULL, &varType, (BYTE*) kbdLayoutText, &dwSize) != ERROR_SUCCESS)
return -1;
RegCloseKey(hKey);
printf("LayoutFile: %s\n", kbdLayoutFile);
printf("LayoutText: %s\n", kbdLayoutText);
pKbdLayerDescriptor = LoadKbdLayerDescriptor(&kbdLibrary, kbdLayoutFile);
if (!pKbdLayerDescriptor)
return -1;
pKbd = pKbdLayerDescriptor();
if (!pKbd)
return -1;
/* Modifier Keys */
i = 0;
for (pVkToBit = pKbd->pCharModifiers->pVkToBit; pVkToBit->Vk != 0; pVkToBit++)
{
printf("pCharModifiers->pVkToBit[%d]: Vk: %s (0x%02X) ModBits: 0x%02X\n", i,
keyboard_get_virtual_key_code_name(pVkToBit->Vk), pVkToBit->Vk, pVkToBit->ModBits);
}
ModNumber = (BYTE*) &(pKbd->pCharModifiers->ModNumber);
printf("pCharModifiers->wMaxModBits: %d\n", pKbd->pCharModifiers->wMaxModBits);
i = 0;
ModNumber = (BYTE*) &pKbd->pCharModifiers->ModNumber;
for (i = 0; i < pKbd->pCharModifiers->wMaxModBits; i++)
{
printf("pCharModifiers->ModNumber[%d]: 0x%02X\n", i, ModNumber[i]);
}
/* Characters */
for (i = 0; pKbd->pVkToWcharTable[i].cbSize; i++)
{
printf("pVkToWcharTable[%d]:\n", i);
printf("\tnModifications: %d\n", pKbd->pVkToWcharTable[i].nModifications);
printf("\tcbSize: %d\n", pKbd->pVkToWcharTable[i].cbSize);
//if (((pKbd->pVkToWcharTable[i].cbSize - 2) / 2) == i)
{
pVkToWchars = pKbd->pVkToWcharTable[i].pVkToWchars;
for (j = 0; pVkToWchars[j].VirtualKey; j++)
{
WCHAR* wch;
printf("\t\tVirtualKey: 0x%02X Attributes: 0x%02X wch: ",
pVkToWchars[j].VirtualKey, pVkToWchars[j].Attributes);
wch = (WCHAR*) &(pVkToWchars[j].wch);
for (k = 0; k < i; k++)
printf("0x%04X ", wch[k]);
printf("\n");
}
}
}
/* Diacritics */
if (pKbd->pDeadKey)
{
i = 0;
for (pDeadKey = pKbd->pDeadKey; pDeadKey->dwBoth != 0; pDeadKey++)
{
printf("DeadKey[%d]: dwBoth: 0x%08X uFlags: 0x%04X wchComposed: 0x%04X\n",
i, pDeadKey->dwBoth, pDeadKey->uFlags, pDeadKey->wchComposed);
i++;
}
}
/* Key Names */
/* Scan Codes to Virtual Keys */
i = 0;
for (pusVSCtoVK = pKbd->pusVSCtoVK; i < (int) pKbd->bMaxVSCtoVK; pusVSCtoVK++)
{
printf("VSCtoVK[%d]: %d\n", i, *pusVSCtoVK);
i++;
}
printf("bMaxVSCtoVK: %d\n", pKbd->bMaxVSCtoVK);
i = 0;
for (pVSCtoVK_E0 = pKbd->pVSCtoVK_E0; i < (int) pKbd->bMaxVSCtoVK; pVSCtoVK_E0++)
{
USHORT vkcode, flags;
vkcode = pVSCtoVK_E0->Vk & 0xFF;
flags = pVSCtoVK_E0->Vk & 0xFF00;
printf("pVSCtoVK_E0[%d]: 0x%02X -> %s (0x%04X) flags: 0x%04X (", i, pVSCtoVK_E0->Vsc,
keyboard_get_virtual_key_code_name(vkcode), vkcode, flags);
PrintVirtualKeyFlags(flags);
printf(")\n");
i++;
}
i = 0;
for (pVSCtoVK_E1 = pKbd->pVSCtoVK_E1; i < (int) pKbd->bMaxVSCtoVK; pVSCtoVK_E1++)
{
USHORT vkcode, flags;
vkcode = pVSCtoVK_E1->Vk & 0xFF;
flags = pVSCtoVK_E1->Vk & 0xFF00;
printf("pVSCtoVK_E1[%d]: 0x%02X -> %s (0x%04X) flags: 0x%04X (", i, pVSCtoVK_E1->Vsc,
keyboard_get_virtual_key_code_name(vkcode), vkcode, flags);
PrintVirtualKeyFlags(flags);
printf(")\n");
i++;
}
/* Locale Flags */
printf("fLocaleFlags: 0x%08X\n", pKbd->fLocaleFlags);
/* Ligatures */
printf("nLgMax: %d\n", pKbd->nLgMax);
printf("cbLgEntry: %d\n", pKbd->cbLgEntry);
/* Type and SubType (Optional) */
printf("dwType: 0x%08X\n", pKbd->dwType);
printf("dwSubType: 0x%08X\n", pKbd->dwSubType);
printf("\n");
if (kbdLibrary)
FreeLibrary(kbdLibrary);
return 0;
}
int ListAllKeyboardLayouts()
{
DWORD i;
HKEY hKey;
DWORD status;
DWORD cbName;
DWORD cSubKeys;
TCHAR achKey[KL_NAMELENGTH];
//PrintKeyboardLayout(_T("00000409")); /* US */
//PrintKeyboardLayout(_T("00011009")); /* CAN */
//return 0;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts"), 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey) != ERROR_SUCCESS)
return -1;
cSubKeys = 0;
status = RegQueryInfoKey(hKey, NULL, NULL, NULL, &cSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
printf( "\nNumber of subkeys: %d\n", cSubKeys);
for (i = 0; i < cSubKeys; i++)
{
cbName = sizeof(achKey) / sizeof(TCHAR);
status = RegEnumKeyEx(hKey, i, achKey, &cbName, NULL, NULL, NULL, NULL);
if (status == ERROR_SUCCESS)
{
_tprintf(_T("%s\n"), achKey);
PrintKeyboardLayout(achKey);
}
}
RegCloseKey(hKey);
return 0;
}
int getKeyboardLayoutFile(TCHAR* layoutFile, DWORD bufferSize)
{
HKEY hKey;
DWORD varType = REG_SZ;
TCHAR kbdName[KL_NAMELENGTH];
TCHAR kbdKeyPath[51 + KL_NAMELENGTH];
ListKeyboardLayouts();
ListAllKeyboardLayouts();
GetKeyboardLayoutName(kbdName);
_stprintf_s(kbdKeyPath, sizeof(kbdKeyPath) / sizeof(TCHAR), _T("SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts\\%s"), kbdName);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR) kbdKeyPath, 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
return -1;
if (RegQueryValueEx(hKey, _T("Layout File"), NULL, &varType, (BYTE*) layoutFile, &bufferSize) != ERROR_SUCCESS)
return -1;
RegCloseKey(hKey);
return 1;
}