-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathKeystroke.cpp
More file actions
606 lines (508 loc) · 17.3 KB
/
Keystroke.cpp
File metadata and controls
606 lines (508 loc) · 17.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
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
// Clavier+
// Keyboard shortcuts manager
//
// Copyright (C) 2000-2008 Guillaume Ryder
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "StdAfx.h"
#include "Keystroke.h"
String Keystroke::s_vk_key_names[256];
BYTE Keystroke::s_next_named_vk[256];
void Keystroke::loadVkKeyNames() {
BYTE last_named_vk = 0xFF;
for (int vk = last_named_vk - 1; vk >= 0; vk--) {
String* key_name = &s_vk_key_names[vk];
loadVkKeyName(BYTE(vk), key_name);
s_next_named_vk[vk] = last_named_vk;
if (key_name->isSome()) {
last_named_vk = BYTE(vk);
}
}
}
void Keystroke::loadVkKeyName(BYTE vk, String* output) {
if (vk <= 0x07
|| (0x0A <= vk && vk <= 0x0B) // reserved
|| vk == 0x5E // reserved
|| (0x88 <= vk && vk <= 0x8F) // reserved
|| (VK_LSHIFT <= vk && vk <= VK_RMENU) // special keys
|| (0xB8 <= vk && vk <= 0xB9) // reserved
|| (0xC1 <= vk && vk <= 0xC2) // reserved
|| (0xC3 <= vk && vk <= 0xDA) // gamepad input
|| vk == 0xE0 // reserved
|| vk == 0xFF) { // reserved & sentinel
return;
}
// GetKeyNameText bug: support VK_F13..VK_F24 manually.
if (VK_F1 <= vk && vk <= VK_F24) {
wsprintf(output->getBuffer(4), _T("F%d"), vk - VK_F1 + 1);
return;
}
// Retrieve the key name with GetKeyNameText.
// Do not distinguish between left and right.
// Fallback to a numerical name like #123 on error,
// for media keys (they have invalid key codes), and for some extended keys
// (to avoid ambiguities like VK_OEM_5 and VK_OEM_102 both named "\").
UINT scan_code = MapVirtualKey(LOBYTE(vk), MAPVK_VK_TO_VSC);
long lParam = (scan_code << 16) | (1L << 25);
if (isKeyExtended(vk)) {
lParam |= 1L << 24;
}
if ((VK_BROWSER_BACK <= vk && vk <= VK_LAUNCH_APP2) || (vk >= 0xE1) ||
!GetKeyNameText(lParam, output->getBuffer(kHotKeyBufSize), kHotKeyBufSize)) {
wsprintf(output->getBuffer(kHotKeyBufSize), _T("#%d"), vk);
}
}
void Keystroke::getDisplayName(LPTSTR output) const {
*output = _T('\0');
// Special keys
for (const auto& special_key : kSpecialKeys) {
if (!(m_sided_mod_code & special_key.all_mod_codes())) {
continue;
}
const LPCTSTR special_key_name = getToken(special_key.tok);
if (m_sided) {
if (m_sided_mod_code & special_key.left_mod_code()) {
StringCchCat(output, kHotKeyBufSize, special_key_name);
StringCchCat(output, kHotKeyBufSize, _T(" "));
StringCchCat(output, kHotKeyBufSize, getToken(Token::kLeft));
StringCchCat(output, kHotKeyBufSize, _T(" + "));
}
if (m_sided_mod_code & special_key.right_mod_code()) {
StringCchCat(output, kHotKeyBufSize, special_key_name);
StringCchCat(output, kHotKeyBufSize, _T(" "));
StringCchCat(output, kHotKeyBufSize, getToken(Token::kRight));
StringCchCat(output, kHotKeyBufSize, _T(" + "));
}
} else {
StringCchCat(output, kHotKeyBufSize, special_key_name);
StringCchCat(output, kHotKeyBufSize, _T(" + "));
}
}
// Main key
StringCchCat(output, kHotKeyBufSize, getKeyName(m_vk));
}
void Keystroke::parseDisplayName(LPTSTR input) {
bool skip_plus = false;
const SpecialKey *current_special_key = nullptr;
for (;;) {
// Skip spaces and '+'
while (*input == _T(' ') || (*input == _T('+') && skip_plus)) {
if (*input == _T('+')) {
current_special_key = nullptr;
skip_plus = false;
}
input++;
}
skip_plus = true;
if (!*input) {
break;
}
// Get the next word
TCHAR *next_word = input;
while (*next_word && *next_word != _T(' ') && *next_word != _T('+')) {
next_word++;
}
const TCHAR next_word_copy = *next_word;
*next_word = _T('\0');
const Token tok = findToken(input);
if (Token::kWin <= tok && tok <= Token::kAlt) {
// Special key token
current_special_key = nullptr;
for (const auto& special_key : kSpecialKeys) {
if (special_key.tok == tok) {
current_special_key = &special_key;
if (!m_sided) {
m_sided_mod_code |= current_special_key->mod_code;
}
break;
}
}
} else if (current_special_key && (tok == Token::kLeft || tok == Token::kRight)) {
// Key side
if (!m_sided) {
m_sided = true;
m_sided_mod_code &= ~current_special_key->all_mod_codes();
}
m_sided_mod_code |= (tok == Token::kLeft)
? current_special_key->left_mod_code()
: current_special_key->right_mod_code();
current_special_key = nullptr;
} else {
// Normal key token
current_special_key = nullptr;
*next_word = next_word_copy;
if (!*input) {
break;
}
for (BYTE vk = s_next_named_vk[0]; vk < 0xFF; vk = s_next_named_vk[vk]) {
if (!lstrcmpi(input, s_vk_key_names[vk])) {
m_vk = canonicalizeKey(vk);
break;
}
}
break;
}
input = next_word;
if (next_word_copy) {
input++;
}
}
}
void Keystroke::simulateTyping(DWORD already_down_mod_code) const {
const bool was_registered = unregisterHotKey();
const DWORD to_press_mod_code = getUnsidedModCode() & ~already_down_mod_code;
// Press the special keys that are not already kept down.
for (const auto& special_key : kSpecialKeys) {
if (to_press_mod_code & special_key.mod_code) {
keybdEvent(special_key.vk, /* down= */ true);
}
}
// Press and release the main key.
keybdEvent(m_vk, /* down= */ true);
sleepBackground(0);
keybdEvent(m_vk, /* down= */ false);
// Release the special keys that should not be kept down.
for (const auto& special_key : kSpecialKeys) {
if (to_press_mod_code & special_key.mod_code) {
keybdEvent(special_key.vk, /* down= */ false);
}
}
if (was_registered) {
registerHotKey();
}
}
void Keystroke::keybdEvent(UINT vk, bool down) {
DWORD dwFlags = down ? 0 : KEYEVENTF_KEYUP;
if (isKeyExtended(vk)) {
dwFlags |= KEYEVENTF_EXTENDEDKEY;
}
keybd_event(BYTE(vk), BYTE(MapVirtualKey(vk, 0)), dwFlags, 0);
}
void Keystroke::registerHotKey() const {
const WORD mod_code = getUnsidedModCode();
if (m_vk == VK_NUMPAD5) {
RegisterHotKey(/* hWnd= */ NULL, MAKEWORD(VK_CLEAR, mod_code), mod_code, VK_CLEAR);
}
RegisterHotKey(/* hWnd= */ NULL, MAKEWORD(m_vk, mod_code), mod_code, m_vk);
}
bool Keystroke::unregisterHotKey() const {
const WORD mod_code = getUnsidedModCode();
if (m_vk == VK_NUMPAD5) {
UnregisterHotKey(/* hWnd= */ NULL, MAKEWORD(VK_CLEAR, mod_code));
}
return toBool(UnregisterHotKey(/* hWnd= */ NULL, MAKEWORD(m_vk, mod_code)));
}
bool Keystroke::isKeyExtended(UINT vk) {
// http://docs.microsoft.com/windows/win32/inputdev/about-keyboard-input#extended-key-flag
return
(VK_PRIOR <= vk && vk <= VK_DOWN) ||
(VK_SNAPSHOT <= vk && vk <= VK_DELETE) ||
vk == VK_DIVIDE || vk == VK_NUMLOCK;
}
bool Keystroke::isSubset(const Keystroke& other) const {
VERIF(m_vk == other.m_vk);
if (m_sided) {
VERIF(other.m_sided && m_sided_mod_code == other.m_sided_mod_code);
} else {
VERIF(getUnsidedModCode() == other.getUnsidedModCode());
}
for (int i = 0; i < kCondTypeCount; i++) {
VERIF(m_conditions[i] == Condition::kIgnore || m_conditions[i] == other.m_conditions[i]);
}
return true;
}
HWND Keystroke::getInputFocus() {
HWND new_input_window;
DWORD new_input_thread;
catchKeyboardFocus(&new_input_window, &new_input_thread);
detachKeyboardFocus(new_input_thread);
return new_input_window;
}
void Keystroke::catchKeyboardFocus(HWND* new_input_window, DWORD* new_input_thread) {
const HWND foreground_window = GetForegroundWindow();
*new_input_thread = GetWindowThreadProcessId(foreground_window, /* lpdwProcessId= */ nullptr);
AttachThreadInput(GetCurrentThreadId(), *new_input_thread, /* fAttach= */ true);
const HWND input_window = GetFocus();
*new_input_window = input_window ? input_window : foreground_window;
}
void Keystroke::detachKeyboardFocus(DWORD input_thread) {
AttachThreadInput(GetCurrentThreadId(), input_thread, /* fAttach= */ false);
}
void Keystroke::resetKeyboardFocus(HWND* new_input_window, DWORD* input_thread) {
detachKeyboardFocus(*input_thread);
catchKeyboardFocus(new_input_window, input_thread);
}
void Keystroke::releaseSpecialKeys(BYTE keyboard_state[], DWORD keep_down_mod_code) {
for (const auto& special_key : kSpecialKeys) {
if (keep_down_mod_code & special_key.mod_code) {
continue;
}
releaseKey(special_key.vk_left, keyboard_state);
releaseKey(special_key.vk_right, keyboard_state);
keyboard_state[special_key.vk] = 0;
}
}
void Keystroke::releaseKey(BYTE vk, BYTE keyboard_state[]) {
if (keyboard_state[vk] & kKeyDownMask) {
keyboard_state[vk] = 0;
keybdEvent(vk, /* down= */ false);
}
}
int Keystroke::compare(const Keystroke& keystroke1, const Keystroke& keystroke2) {
// Compare virtual key codes
const int vk1 = int(keystroke1.m_vk);
const int vk2 = int(keystroke2.m_vk);
if (vk1 != vk2) {
return vk1 - vk2;
}
// Compare special keys, applying the following order:
// X
// Win + X
// Win + Ctrl + X
// Win + Ctrl + Shift + X
// Win + Shift + X
// Win Left + Shift Left + X
// Win Left + Shift Right + X
// Win Right + Shift Left + X
// Win Right + Shift Right + X
// Win Left + Win Right + Sift Left + X
// Ctrl + X
// Ctrl + Shift + X
// Ctrl + Shift + Alt + X
// Ctrl + Alt + X
// Ctrl Left + X
// Shift + X
// Alt + X
DWORD remaining_mod_codes_mask = kModCodeAll;
for (const auto& special_key : kSpecialKeys) {
remaining_mod_codes_mask &= ~special_key.all_mod_codes();
const int sort_key1 = keystroke1.getModCodeSortKey(special_key, remaining_mod_codes_mask);
const int sort_key2 = keystroke2.getModCodeSortKey(special_key, remaining_mod_codes_mask);
if (sort_key1 != sort_key2) {
return sort_key1 - sort_key2;
}
}
return 0;
}
// Return values:
// 0: the keystroke does not use the special key
// and has none of the mod codes in remaining_mod_codes_mask.
// 1: the keystroke uses the given special key unsided.
// 2: the keystroke uses the given special key left-sided only.
// 4: the keystroke uses the given special key right-sided only.
// 6: the keystroke uses the given special key both left- and right-sided.
// 8: the keystroke does not use the special key
// and has some of the mod codes in remaining_mod_codes_mask.
int Keystroke::getModCodeSortKey(const SpecialKey& special_key, DWORD remaining_mod_codes_mask) const {
if (m_sided) {
int result = 0;
if (m_sided_mod_code & special_key.left_mod_code()) {
// Left
result |= 2;
}
if (m_sided_mod_code & special_key.right_mod_code()) {
// Right
result |= 4;
}
if (result) {
return result;
}
} else if (m_sided_mod_code & special_key.all_mod_codes()) {
// Unsided
return 1;
}
// None
return (m_sided_mod_code & remaining_mod_codes_mask) ? 8 : 0;
}
namespace {
// True if the currently edited keystroke should be reset ASAP.
// The keystroke is reset when new keys are pressed after all special keys have been released.
bool s_reset_keystroke;
// Currently edited keystroke
Keystroke s_keystroke;
// Keystroke control in prcEditDialog.
LRESULT CALLBACK prcKeystrokeCtl(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR UNUSED(subclass_id), DWORD_PTR UNUSED(ref_data)) {
bool down = false;
switch (message) {
case WM_LBUTTONDOWN:
SetFocus(hwnd);
break;
case WM_SETCURSOR:
SetCursor(LoadCursor(/* hInstance= */ NULL, IDC_ARROW));
return true;
case WM_GETDLGCODE:
return DLGC_WANTALLKEYS;
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (s_reset_keystroke) {
s_keystroke.clearKeys();
}
down = true;
// Fall-through
case WM_KEYUP:
case WM_SYSKEYUP: {
BYTE vk_typed = BYTE(wParam);
// Flags
DWORD sided_mod_code = s_keystroke.m_sided_mod_code;
bool is_special = false;
for (const auto& special_key : kSpecialKeys) {
if ((vk_typed == special_key.vk) ||
(vk_typed == special_key.vk_left) ||
(vk_typed == special_key.vk_right)) {
vk_typed = special_key.vk;
is_special = true;
}
if (isKeyDown(special_key.vk_left)) {
sided_mod_code |= special_key.left_mod_code();
} else {
sided_mod_code &= ~special_key.left_mod_code();
}
if (isKeyDown(special_key.vk_right)) {
sided_mod_code |= special_key.right_mod_code();
} else {
sided_mod_code &= ~special_key.right_mod_code();
}
}
// Key
// If key is pressed, update the flags
// If key is released, update the flags only if no key has been set
bool update_mode_code = true;
if (down) {
s_reset_keystroke = false;
if (!is_special) {
s_keystroke.m_vk = Keystroke::canonicalizeKey(vk_typed);
}
} else {
s_reset_keystroke |= !is_special;
update_mode_code &= !s_keystroke.m_vk;
}
if (update_mode_code) {
s_keystroke.m_sided_mod_code = sided_mod_code;
}
}
// Fall-through
// Display keystroke name
case WM_KEYSTROKE: {
TCHAR keystroke_display_name[kHotKeyBufSize];
s_keystroke.getDisplayName(keystroke_display_name);
SetWindowText(hwnd, keystroke_display_name);
const int len = lstrlen(keystroke_display_name);
Edit_SetSel(hwnd, len,len);
return 0;
}
}
// Ignore mouse and keyboard events
if ((WM_MOUSEFIRST <= message && message <= WM_MOUSELAST)
|| (WM_KEYFIRST <= message && message <= WM_KEYLAST)) {
return 0;
}
return DefSubclassProc(hwnd, message, wParam, lParam);
}
INT_PTR CALLBACK prcEditDialog(HWND hdlg, UINT message, WPARAM wParam, LPARAM UNUSED(lParam)) {
switch (message) {
// Initialization
case WM_INITDIALOG: {
e_modal_dialog = hdlg;
centerParent(hdlg);
// Subclass the keystroke control. Display the initial keystroke name.
const HWND hwnd_keystroke = GetDlgItem(hdlg, IDCTXT);
subclassWindow(hwnd_keystroke, prcKeystrokeCtl);
PostMessage(hwnd_keystroke, WM_KEYSTROKE, 0,0);
CheckDlgButton(hdlg, IDCCHK_DISTINGUISH_LEFT_RIGHT, s_keystroke.m_sided);
// Load ';'-separated condition names. Make them null-terminated strings.
TCHAR conditions[kStringBufSize];
i18n::loadStringAuto(IDS_CONDITIONS, conditions);
TCHAR* current_condition = conditions;
for (int cond = 0; cond < Keystroke::kConditionCount; cond++) {
getSemiColonToken(¤t_condition);
}
// Initialize the condition combo-boxes
for (int cond_type = 0; cond_type < Keystroke::kCondTypeCount; cond_type++) {
const HWND hwnd_cond_type = GetDlgItem(hdlg, IDCCBO_CAPSLOCK + cond_type);
current_condition = conditions;
for (int cond = 0; cond < Keystroke::kConditionCount; cond++) {
ComboBox_AddString(hwnd_cond_type, current_condition);
current_condition += lstrlen(current_condition) + 1;
}
ComboBox_SetCurSel(hwnd_cond_type, s_keystroke.m_conditions[cond_type]);
}
return true;
}
// Command
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDCCHK_DISTINGUISH_LEFT_RIGHT:
s_keystroke.m_sided = toBool(IsDlgButtonChecked(hdlg, IDCCHK_DISTINGUISH_LEFT_RIGHT));
PostMessage(GetDlgItem(hdlg, IDCTXT), WM_KEYSTROKE, 0,0);
break;
case IDOK: {
if (!s_keystroke.m_vk) {
messageBox(hdlg, ERR_INVALID_SHORTCUT, MB_ICONEXCLAMATION);
SetFocus(GetDlgItem(hdlg, IDCTXT));
break;
}
for (int i = 0; i < Keystroke::kCondTypeCount; i++) {
s_keystroke.m_conditions[i] =
Keystroke::Condition(ComboBox_GetCurSel(GetDlgItem(hdlg, IDCCBO_CAPSLOCK + i)));
}
}
// Fall-through
case IDCANCEL:
EndDialog(hdlg, LOWORD(wParam));
break;
}
break;
}
return 0;
}
} // namespace
bool Keystroke::showEditDialog(HWND hwnd_parent) {
s_reset_keystroke = true;
s_keystroke = *this;
VERIF(IDOK == i18n::dialogBox(IDD_KEYSTROKE, hwnd_parent, prcEditDialog));
*this = s_keystroke;
return true;
}
namespace {
INT_PTR CALLBACK prcSendKeysDialog(HWND hdlg, UINT message, WPARAM wParam, LPARAM UNUSED(lParam)) {
switch (message) {
// Initialization
case WM_INITDIALOG: {
e_modal_dialog = hdlg;
centerParent(hdlg);
const HWND hctl = GetDlgItem(hdlg, IDCTXT);
subclassWindow(hctl, prcKeystrokeCtl);
PostMessage(hctl, WM_KEYSTROKE, 0,0);
return true;
}
// Command
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
case IDCANCEL:
EndDialog(hdlg, LOWORD(wParam));
break;
}
break;
}
return 0;
}
} // namespace
bool Keystroke::showSendKeysDialog(HWND hwnd_parent) {
s_keystroke.clearKeys();
s_keystroke.m_sided = false;
VERIF(IDOK == i18n::dialogBox(IDD_SENDKEYS, hwnd_parent, prcSendKeysDialog));
*this = s_keystroke;
return true;
}