@vscodium/native-keymap 3.3.7-258412

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/.nvmrc +1 -0
  2. package/.release-it.yml +8 -0
  3. package/LICENSE +22 -0
  4. package/README.md +82 -0
  5. package/ThirdPartyNotices.txt +42 -0
  6. package/binding.gyp +92 -0
  7. package/collected-artifacts/native-keymap-darwin-arm64/native-keymap-3.3.7-258412-darwin-arm64.tar.gz +0 -0
  8. package/collected-artifacts/native-keymap-darwin-arm64/native-keymap-3.3.7-258412-darwin-arm64.tar.gz.sha256 +1 -0
  9. package/collected-artifacts/native-keymap-darwin-x64/native-keymap-3.3.7-258412-darwin-x64.tar.gz +0 -0
  10. package/collected-artifacts/native-keymap-darwin-x64/native-keymap-3.3.7-258412-darwin-x64.tar.gz.sha256 +1 -0
  11. package/collected-artifacts/native-keymap-linux-arm64/native-keymap-3.3.7-258412-linux-arm64.tar.gz +0 -0
  12. package/collected-artifacts/native-keymap-linux-arm64/native-keymap-3.3.7-258412-linux-arm64.tar.gz.sha256 +1 -0
  13. package/collected-artifacts/native-keymap-linux-armhf/native-keymap-3.3.7-258412-linux-armhf.tar.gz +0 -0
  14. package/collected-artifacts/native-keymap-linux-armhf/native-keymap-3.3.7-258412-linux-armhf.tar.gz.sha256 +1 -0
  15. package/collected-artifacts/native-keymap-linux-loong64/native-keymap-3.3.7-258412-linux-loong64.tar.gz +0 -0
  16. package/collected-artifacts/native-keymap-linux-loong64/native-keymap-3.3.7-258412-linux-loong64.tar.gz.sha256 +1 -0
  17. package/collected-artifacts/native-keymap-linux-ppc64le/native-keymap-3.3.7-258412-linux-ppc64le.tar.gz +0 -0
  18. package/collected-artifacts/native-keymap-linux-ppc64le/native-keymap-3.3.7-258412-linux-ppc64le.tar.gz.sha256 +1 -0
  19. package/collected-artifacts/native-keymap-linux-riscv64/native-keymap-3.3.7-258412-linux-riscv64.tar.gz +0 -0
  20. package/collected-artifacts/native-keymap-linux-riscv64/native-keymap-3.3.7-258412-linux-riscv64.tar.gz.sha256 +1 -0
  21. package/collected-artifacts/native-keymap-linux-s390x/native-keymap-3.3.7-258412-linux-s390x.tar.gz +0 -0
  22. package/collected-artifacts/native-keymap-linux-s390x/native-keymap-3.3.7-258412-linux-s390x.tar.gz.sha256 +1 -0
  23. package/collected-artifacts/native-keymap-linux-x64/native-keymap-3.3.7-258412-linux-x64.tar.gz +0 -0
  24. package/collected-artifacts/native-keymap-linux-x64/native-keymap-3.3.7-258412-linux-x64.tar.gz.sha256 +1 -0
  25. package/collected-artifacts/native-keymap-win32-arm64/native-keymap-3.3.7-258412-win32-arm64.tar.gz +0 -0
  26. package/collected-artifacts/native-keymap-win32-arm64/native-keymap-3.3.7-258412-win32-arm64.tar.gz.sha256 +1 -0
  27. package/collected-artifacts/native-keymap-win32-x64/native-keymap-3.3.7-258412-win32-x64.tar.gz +0 -0
  28. package/collected-artifacts/native-keymap-win32-x64/native-keymap-3.3.7-258412-win32-x64.tar.gz.sha256 +1 -0
  29. package/deps/chromium/dom_code_data.inc +618 -0
  30. package/deps/chromium/keyboard_codes.h +23 -0
  31. package/deps/chromium/macros.h +32 -0
  32. package/deps/chromium/x/keysym_to_unicode.cc +896 -0
  33. package/deps/chromium/x/keysym_to_unicode.h +26 -0
  34. package/index.d.ts +70 -0
  35. package/index.js +70 -0
  36. package/package.json +25 -0
  37. package/release/native-keymap-3.3.7-258412-darwin-arm64.tar.gz +0 -0
  38. package/release/native-keymap-3.3.7-258412-darwin-arm64.tar.gz.sha256 +1 -0
  39. package/release/native-keymap-3.3.7-258412-darwin-x64.tar.gz +0 -0
  40. package/release/native-keymap-3.3.7-258412-darwin-x64.tar.gz.sha256 +1 -0
  41. package/release/native-keymap-3.3.7-258412-linux-arm64.tar.gz +0 -0
  42. package/release/native-keymap-3.3.7-258412-linux-arm64.tar.gz.sha256 +1 -0
  43. package/release/native-keymap-3.3.7-258412-linux-armhf.tar.gz +0 -0
  44. package/release/native-keymap-3.3.7-258412-linux-armhf.tar.gz.sha256 +1 -0
  45. package/release/native-keymap-3.3.7-258412-linux-loong64.tar.gz +0 -0
  46. package/release/native-keymap-3.3.7-258412-linux-loong64.tar.gz.sha256 +1 -0
  47. package/release/native-keymap-3.3.7-258412-linux-ppc64le.tar.gz +0 -0
  48. package/release/native-keymap-3.3.7-258412-linux-ppc64le.tar.gz.sha256 +1 -0
  49. package/release/native-keymap-3.3.7-258412-linux-riscv64.tar.gz +0 -0
  50. package/release/native-keymap-3.3.7-258412-linux-riscv64.tar.gz.sha256 +1 -0
  51. package/release/native-keymap-3.3.7-258412-linux-s390x.tar.gz +0 -0
  52. package/release/native-keymap-3.3.7-258412-linux-s390x.tar.gz.sha256 +1 -0
  53. package/release/native-keymap-3.3.7-258412-linux-x64.tar.gz +0 -0
  54. package/release/native-keymap-3.3.7-258412-linux-x64.tar.gz.sha256 +1 -0
  55. package/release/native-keymap-3.3.7-258412-win32-arm64.tar.gz +0 -0
  56. package/release/native-keymap-3.3.7-258412-win32-arm64.tar.gz.sha256 +1 -0
  57. package/release/native-keymap-3.3.7-258412-win32-x64.tar.gz +0 -0
  58. package/release/native-keymap-3.3.7-258412-win32-x64.tar.gz.sha256 +1 -0
  59. package/src/common.h +92 -0
  60. package/src/keyboard_mac.mm +209 -0
  61. package/src/keyboard_win.cc +476 -0
  62. package/src/keyboard_x.cc +407 -0
  63. package/src/keymapping.cc +147 -0
  64. package/src/keymapping.h +65 -0
  65. package/src/string_conversion.cc +67 -0
  66. package/src/string_conversion.h +18 -0
@@ -0,0 +1,476 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+
6
+ #include "keymapping.h"
7
+ #include "common.h"
8
+
9
+ #include "../deps/chromium/macros.h"
10
+
11
+ #include "string_conversion.h"
12
+ #include <windows.h>
13
+ #include <msctf.h>
14
+ #include <ime.h>
15
+
16
+ namespace {
17
+
18
+ void ClearKeyboardBuffer(UINT key_code, UINT scan_code, BYTE* keyboard_state) {
19
+ memset(keyboard_state, 0, 256);
20
+
21
+ wchar_t chars[5];
22
+ int code = 0;
23
+ do {
24
+ code = ::ToUnicode(key_code, scan_code, keyboard_state, chars, 4, 0);
25
+ } while (code < 0);
26
+ }
27
+
28
+ std::string GetStrFromKeyPress(UINT key_code, int modifiers, BYTE *keyboard_state, UINT clear_key_code, UINT clear_scan_code) {
29
+ memset(keyboard_state, 0, 256);
30
+
31
+ if (modifiers & kShiftKeyModifierMask) {
32
+ keyboard_state[VK_SHIFT] |= 0x80;
33
+ }
34
+
35
+ if (modifiers & kControlKeyModifierMask) {
36
+ keyboard_state[VK_CONTROL] |= 0x80;
37
+ }
38
+
39
+ if (modifiers & kAltKeyModifierMask) {
40
+ keyboard_state[VK_MENU] |= 0x80;
41
+ }
42
+
43
+ UINT scan_code = ::MapVirtualKeyW(key_code, MAPVK_VK_TO_VSC);
44
+
45
+ wchar_t chars[5];
46
+ int code = ::ToUnicode(key_code, scan_code, keyboard_state, chars, 4, 0);
47
+
48
+ if (code == -1) {
49
+ // dead key
50
+ if (chars[0] == 0 || iswcntrl(chars[0])) {
51
+ return std::string();
52
+ }
53
+ code = 1;
54
+ }
55
+
56
+ ClearKeyboardBuffer(clear_key_code, clear_scan_code, keyboard_state);
57
+
58
+ if (code <= 0 || (code == 1 && iswcntrl(chars[0]))) {
59
+ return std::string();
60
+ }
61
+
62
+ return vscode_keyboard::UTF16toUTF8(chars, code);
63
+ }
64
+
65
+ } // namespace
66
+
67
+ namespace vscode_keyboard {
68
+
69
+ #define DOM_CODE(usb, evdev, xkb, win, mac, code, id) {usb, win, code}
70
+ #define DOM_CODE_DECLARATION const KeycodeMapEntry usb_keycode_map[] =
71
+ #include "../deps/chromium/dom_code_data.inc"
72
+ #undef DOM_CODE
73
+ #undef DOM_CODE_DECLARATION
74
+
75
+ typedef struct {
76
+ int vkey;
77
+ const char* str_vkey;
78
+ } VKeyStrEntry;
79
+
80
+ const char* VKeyToStr(int vkey) {
81
+ switch (vkey) {
82
+ case VK_LBUTTON: return "VK_LBUTTON"; // Left mouse button
83
+ case VK_RBUTTON: return "VK_RBUTTON"; // Right mouse button
84
+ case VK_CANCEL: return "VK_CANCEL"; // Control-break processing
85
+ case VK_MBUTTON: return "VK_MBUTTON"; // Middle mouse button (three-button mouse)
86
+ case VK_XBUTTON1: return "VK_XBUTTON1"; // X1 mouse button
87
+ case VK_XBUTTON2: return "VK_XBUTTON2"; // X2 mouse button
88
+ case VK_BACK: return "VK_BACK"; // BACKSPACE key
89
+ case VK_TAB: return "VK_TAB"; // TAB key
90
+ case VK_CLEAR: return "VK_CLEAR"; // CLEAR key
91
+ case VK_RETURN: return "VK_RETURN"; // ENTER key
92
+ case VK_SHIFT: return "VK_SHIFT"; // SHIFT key
93
+ case VK_CONTROL: return "VK_CONTROL"; // CTRL key
94
+ case VK_MENU: return "VK_MENU"; // ALT key
95
+ case VK_PAUSE: return "VK_PAUSE"; // PAUSE key
96
+ case VK_CAPITAL: return "VK_CAPITAL"; // CAPS LOCK key
97
+ case VK_KANA: return "VK_KANA"; // IME Kana mode
98
+ //case VK_HANGUL: return "VK_HANGUEL"; // IME Hangul mode
99
+ case VK_JUNJA: return "VK_JUNJA"; // IME Junja mode
100
+ case VK_FINAL: return "VK_FINAL"; // IME final mode
101
+ case VK_HANJA: return "VK_HANJA"; // IME Hanja mode
102
+ //case VK_KANJI: return "VK_KANJI"; // IME Kanji mode
103
+ case VK_ESCAPE: return "VK_ESCAPE"; // ESC key
104
+ case VK_CONVERT: return "VK_CONVERT"; // IME convert
105
+ case VK_NONCONVERT: return "VK_NONCONVERT"; // IME nonconvert
106
+ case VK_ACCEPT: return "VK_ACCEPT"; // IME accept
107
+ case VK_MODECHANGE: return "VK_MODECHANGE"; // IME mode change request
108
+ case VK_SPACE: return "VK_SPACE"; // SPACEBAR
109
+ case VK_PRIOR: return "VK_PRIOR"; // PAGE UP key
110
+ case VK_NEXT: return "VK_NEXT"; // PAGE DOWN key
111
+ case VK_END: return "VK_END"; // END key
112
+ case VK_HOME: return "VK_HOME"; // HOME key
113
+ case VK_LEFT: return "VK_LEFT"; // LEFT ARROW key
114
+ case VK_UP: return "VK_UP"; // UP ARROW key
115
+ case VK_RIGHT: return "VK_RIGHT"; // RIGHT ARROW key
116
+ case VK_DOWN: return "VK_DOWN"; // DOWN ARROW key
117
+ case VK_SELECT: return "VK_SELECT"; // SELECT key
118
+ case VK_PRINT: return "VK_PRINT"; // PRINT key
119
+ case VK_EXECUTE: return "VK_EXECUTE"; // EXECUTE key
120
+ case VK_SNAPSHOT: return "VK_SNAPSHOT"; // PRINT SCREEN key
121
+ case VK_INSERT: return "VK_INSERT"; // INS key
122
+ case VK_DELETE: return "VK_DELETE"; // DEL key
123
+ case VK_HELP: return "VK_HELP"; // HELP key
124
+
125
+ case '0': return "VK_0";
126
+ case '1': return "VK_1";
127
+ case '2': return "VK_2";
128
+ case '3': return "VK_3";
129
+ case '4': return "VK_4";
130
+ case '5': return "VK_5";
131
+ case '6': return "VK_6";
132
+ case '7': return "VK_7";
133
+ case '8': return "VK_8";
134
+ case '9': return "VK_9";
135
+ case 'A': return "VK_A";
136
+ case 'B': return "VK_B";
137
+ case 'C': return "VK_C";
138
+ case 'D': return "VK_D";
139
+ case 'E': return "VK_E";
140
+ case 'F': return "VK_F";
141
+ case 'G': return "VK_G";
142
+ case 'H': return "VK_H";
143
+ case 'I': return "VK_I";
144
+ case 'J': return "VK_J";
145
+ case 'K': return "VK_K";
146
+ case 'L': return "VK_L";
147
+ case 'M': return "VK_M";
148
+ case 'N': return "VK_N";
149
+ case 'O': return "VK_O";
150
+ case 'P': return "VK_P";
151
+ case 'Q': return "VK_Q";
152
+ case 'R': return "VK_R";
153
+ case 'S': return "VK_S";
154
+ case 'T': return "VK_T";
155
+ case 'U': return "VK_U";
156
+ case 'V': return "VK_V";
157
+ case 'W': return "VK_W";
158
+ case 'X': return "VK_X";
159
+ case 'Y': return "VK_Y";
160
+ case 'Z': return "VK_Z";
161
+
162
+ case VK_LWIN: return "VK_LWIN"; // Left Windows key (Natural keyboard)
163
+ case VK_RWIN: return "VK_RWIN"; // Right Windows key (Natural keyboard)
164
+ case VK_APPS: return "VK_APPS"; // Applications key (Natural keyboard)
165
+ case VK_SLEEP: return "VK_SLEEP"; // Computer Sleep key
166
+ case VK_NUMPAD0: return "VK_NUMPAD0"; // Numeric keypad 0 key
167
+ case VK_NUMPAD1: return "VK_NUMPAD1"; // Numeric keypad 1 key
168
+ case VK_NUMPAD2: return "VK_NUMPAD2"; // Numeric keypad 2 key
169
+ case VK_NUMPAD3: return "VK_NUMPAD3"; // Numeric keypad 3 key
170
+ case VK_NUMPAD4: return "VK_NUMPAD4"; // Numeric keypad 4 key
171
+ case VK_NUMPAD5: return "VK_NUMPAD5"; // Numeric keypad 5 key
172
+ case VK_NUMPAD6: return "VK_NUMPAD6"; // Numeric keypad 6 key
173
+ case VK_NUMPAD7: return "VK_NUMPAD7"; // Numeric keypad 7 key
174
+ case VK_NUMPAD8: return "VK_NUMPAD8"; // Numeric keypad 8 key
175
+ case VK_NUMPAD9: return "VK_NUMPAD9"; // Numeric keypad 9 key
176
+ case VK_MULTIPLY: return "VK_MULTIPLY"; // Multiply key
177
+ case VK_ADD: return "VK_ADD"; // Add key
178
+ case VK_SEPARATOR: return "VK_SEPARATOR"; // Separator key
179
+ case VK_SUBTRACT: return "VK_SUBTRACT"; // Subtract key
180
+ case VK_DECIMAL: return "VK_DECIMAL"; // Decimal key
181
+ case VK_DIVIDE: return "VK_DIVIDE"; // Divide key
182
+ case VK_F1: return "VK_F1"; // F1 key
183
+ case VK_F2: return "VK_F2"; // F2 key
184
+ case VK_F3: return "VK_F3"; // F3 key
185
+ case VK_F4: return "VK_F4"; // F4 key
186
+ case VK_F5: return "VK_F5"; // F5 key
187
+ case VK_F6: return "VK_F6"; // F6 key
188
+ case VK_F7: return "VK_F7"; // F7 key
189
+ case VK_F8: return "VK_F8"; // F8 key
190
+ case VK_F9: return "VK_F9"; // F9 key
191
+ case VK_F10: return "VK_F10"; // F10 key
192
+ case VK_F11: return "VK_F11"; // F11 key
193
+ case VK_F12: return "VK_F12"; // F12 key
194
+ case VK_F13: return "VK_F13"; // F13 key
195
+ case VK_F14: return "VK_F14"; // F14 key
196
+ case VK_F15: return "VK_F15"; // F15 key
197
+ case VK_F16: return "VK_F16"; // F16 key
198
+ case VK_F17: return "VK_F17"; // F17 key
199
+ case VK_F18: return "VK_F18"; // F18 key
200
+ case VK_F19: return "VK_F19"; // F19 key
201
+ case VK_F20: return "VK_F20"; // F20 key
202
+ case VK_F21: return "VK_F21"; // F21 key
203
+ case VK_F22: return "VK_F22"; // F22 key
204
+ case VK_F23: return "VK_F23"; // F23 key
205
+ case VK_F24: return "VK_F24"; // F24 key
206
+ case VK_NUMLOCK: return "VK_NUMLOCK"; // NUM LOCK key
207
+ case VK_SCROLL: return "VK_SCROLL"; // SCROLL LOCK key
208
+ case VK_LSHIFT: return "VK_LSHIFT"; // Left SHIFT key
209
+ case VK_RSHIFT: return "VK_RSHIFT"; // Right SHIFT key
210
+ case VK_LCONTROL: return "VK_LCONTROL"; // Left CONTROL key
211
+ case VK_RCONTROL: return "VK_RCONTROL"; // Right CONTROL key
212
+ case VK_LMENU: return "VK_LMENU"; // Left MENU key
213
+ case VK_RMENU: return "VK_RMENU"; // Right MENU key
214
+ case VK_BROWSER_BACK: return "VK_BROWSER_BACK"; // Browser Back key
215
+ case VK_BROWSER_FORWARD: return "VK_BROWSER_FORWARD"; // Browser Forward key
216
+ case VK_BROWSER_REFRESH: return "VK_BROWSER_REFRESH"; // Browser Refresh key
217
+ case VK_BROWSER_STOP: return "VK_BROWSER_STOP"; // Browser Stop key
218
+ case VK_BROWSER_SEARCH: return "VK_BROWSER_SEARCH"; // Browser Search key
219
+ case VK_BROWSER_FAVORITES: return "VK_BROWSER_FAVORITES"; // Browser Favorites key
220
+ case VK_BROWSER_HOME: return "VK_BROWSER_HOME"; // Browser Start and Home key
221
+ case VK_VOLUME_MUTE: return "VK_VOLUME_MUTE"; // Volume Mute key
222
+ case VK_VOLUME_DOWN: return "VK_VOLUME_DOWN"; // Volume Down key
223
+ case VK_VOLUME_UP: return "VK_VOLUME_UP"; // Volume Up key
224
+ case VK_MEDIA_NEXT_TRACK: return "VK_MEDIA_NEXT_TRACK"; // Next Track key
225
+ case VK_MEDIA_PREV_TRACK: return "VK_MEDIA_PREV_TRACK"; // Previous Track key
226
+ case VK_MEDIA_STOP: return "VK_MEDIA_STOP"; // Stop Media key
227
+ case VK_MEDIA_PLAY_PAUSE: return "VK_MEDIA_PLAY_PAUSE"; // Play/Pause Media key
228
+ case VK_LAUNCH_MAIL: return "VK_LAUNCH_MAIL"; // Start Mail key
229
+ case VK_LAUNCH_MEDIA_SELECT: return "VK_LAUNCH_MEDIA_SELECT"; // Select Media key
230
+ case VK_LAUNCH_APP1: return "VK_LAUNCH_APP1"; // Start Application 1 key
231
+ case VK_LAUNCH_APP2: return "VK_LAUNCH_APP2"; // Start Application 2 key
232
+ case VK_OEM_1: return "VK_OEM_1"; // Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ';:' key
233
+ case VK_OEM_PLUS: return "VK_OEM_PLUS"; // For any country/region, the '+' key
234
+ case VK_OEM_COMMA: return "VK_OEM_COMMA"; // For any country/region, the ',' key
235
+ case VK_OEM_MINUS: return "VK_OEM_MINUS"; // For any country/region, the '-' key
236
+ case VK_OEM_PERIOD: return "VK_OEM_PERIOD"; // For any country/region, the '.' key
237
+ case VK_OEM_2: return "VK_OEM_2"; // Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '/?' key
238
+ case VK_OEM_3: return "VK_OEM_3"; // Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '`~' key
239
+ case 0xC1: return "VK_ABNT_C1"; // Brazilian (ABNT) Keyboard
240
+ case 0xC2: return "VK_ABNT_C2"; // Brazilian (ABNT) Keyboard
241
+ case VK_OEM_4: return "VK_OEM_4"; // Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '[{' key
242
+ case VK_OEM_5: return "VK_OEM_5"; // Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '\|' key
243
+ case VK_OEM_6: return "VK_OEM_6"; // Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ']}' key
244
+ case VK_OEM_7: return "VK_OEM_7"; // Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the 'single-quote/double-quote' key
245
+ case VK_OEM_8: return "VK_OEM_8"; // Used for miscellaneous characters; it can vary by keyboard.
246
+ case VK_OEM_102: return "VK_OEM_102"; // Either the angle bracket key or the backslash key on the RT 102-key keyboard
247
+ case VK_PROCESSKEY: return "VK_PROCESSKEY"; // IME PROCESS key
248
+ case VK_PACKET: return "VK_PACKET"; // Used to pass Unicode characters as if they were keystrokes. The VK_PACKET key is the low word of a 32-bit Virtual Key value used for non-keyboard input methods. For more information, see Remark in KEYBDINPUT, SendInput, WM_KEYDOWN, and WM_KEYUP // 0xE8
249
+ case VK_ATTN: return "VK_ATTN"; // Attn key
250
+ case VK_CRSEL: return "VK_CRSEL"; // CrSel key
251
+ case VK_EXSEL: return "VK_EXSEL"; // ExSel key
252
+ case VK_EREOF: return "VK_EREOF"; // Erase EOF key
253
+ case VK_PLAY: return "VK_PLAY"; // Play key
254
+ case VK_ZOOM: return "VK_ZOOM"; // Zoom key
255
+ case VK_NONAME: return "VK_NONAME"; // Reserved
256
+ case VK_PA1: return "VK_PA1"; // PA1 key
257
+ case VK_OEM_CLEAR: return "VK_OEM_CLEAR"; // Clear key
258
+ }
259
+
260
+ return "VK_UNKNOWN";
261
+ }
262
+
263
+ class UseForegroundKeyboardLayoutScope {
264
+ public:
265
+ UseForegroundKeyboardLayoutScope() : original_layout_(GetKeyboardLayout(0)) {
266
+ if (auto window = GetForegroundWindow()) {
267
+ const auto thread_id = GetWindowThreadProcessId(window, nullptr);
268
+ ActivateKeyboardLayout(GetKeyboardLayout(thread_id), 0);
269
+ }
270
+ }
271
+
272
+ ~UseForegroundKeyboardLayoutScope() {
273
+ ActivateKeyboardLayout(original_layout_, 0);
274
+ }
275
+
276
+ UseForegroundKeyboardLayoutScope(const UseForegroundKeyboardLayoutScope&) = delete;
277
+ UseForegroundKeyboardLayoutScope& operator=(const UseForegroundKeyboardLayoutScope&) = delete;
278
+
279
+ private:
280
+ HKL original_layout_ = nullptr;
281
+ };
282
+
283
+ napi_value GetKeyMapImpl(napi_env env, napi_callback_info info) {
284
+ UseForegroundKeyboardLayoutScope use_foreground_keyboard_layout;
285
+
286
+ napi_value result;
287
+ NAPI_CALL(env, napi_create_object(env, &result));
288
+
289
+ UINT clear_key_code = VK_DECIMAL;
290
+ UINT clear_scan_code = ::MapVirtualKeyW(clear_key_code, MAPVK_VK_TO_VSC);
291
+ BYTE keyboard_state[256];
292
+
293
+ size_t cnt = sizeof(usb_keycode_map) / sizeof(usb_keycode_map[0]);
294
+ for (size_t i = 0; i < cnt; ++i) {
295
+ const char *code = usb_keycode_map[i].code;
296
+ int native_scancode = usb_keycode_map[i].native_keycode;
297
+
298
+ if (!code || native_scancode <= 0) {
299
+ continue;
300
+ }
301
+
302
+ int native_keycode = ::MapVirtualKeyW(native_scancode, MAPVK_VSC_TO_VK);
303
+
304
+ napi_value entry;
305
+ NAPI_CALL(env, napi_create_object(env, &entry));
306
+
307
+ NAPI_CALL(env, napi_set_named_property_string_utf8(env, entry, "vkey", VKeyToStr(native_keycode)));
308
+
309
+ std::string value = GetStrFromKeyPress(native_keycode, 0, keyboard_state, clear_key_code, clear_scan_code);
310
+ NAPI_CALL(env, napi_set_named_property_string_utf8(env, entry, "value", value.c_str()));
311
+
312
+ std::string with_shift = GetStrFromKeyPress(native_keycode, kShiftKeyModifierMask, keyboard_state, clear_key_code, clear_scan_code);
313
+ NAPI_CALL(env, napi_set_named_property_string_utf8(env, entry, "withShift", with_shift.c_str()));
314
+
315
+ std::string with_alt_gr = GetStrFromKeyPress(native_keycode, kControlKeyModifierMask | kAltKeyModifierMask, keyboard_state, clear_key_code, clear_scan_code);
316
+ NAPI_CALL(env, napi_set_named_property_string_utf8(env, entry, "withAltGr", with_alt_gr.c_str()));
317
+
318
+ std::string with_shift_alt_gr = GetStrFromKeyPress(native_keycode, kShiftKeyModifierMask | kControlKeyModifierMask | kAltKeyModifierMask, keyboard_state, clear_key_code, clear_scan_code);
319
+ NAPI_CALL(env, napi_set_named_property_string_utf8(env, entry, "withShiftAltGr", with_shift_alt_gr.c_str()));
320
+
321
+ NAPI_CALL(env, napi_set_named_property(env, result, code, entry));
322
+ }
323
+ return result;
324
+ }
325
+
326
+ std::string GetStringRegKey(std::string path, std::string name) {
327
+ std::string result = "";
328
+
329
+ HKEY hKey;
330
+ if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, path.c_str(), 0, KEY_READ, &hKey)) {
331
+ return result;
332
+ }
333
+
334
+ char szBuffer[512];
335
+ DWORD dwBufferSize = sizeof(szBuffer);
336
+
337
+ if (ERROR_SUCCESS == RegQueryValueEx(hKey, name.c_str(), 0, NULL, (LPBYTE)szBuffer, &dwBufferSize)) {
338
+ result = szBuffer;
339
+ }
340
+
341
+ RegCloseKey(hKey);
342
+
343
+ return result;
344
+ }
345
+
346
+ napi_value GetCurrentKeyboardLayoutImpl(napi_env env, napi_callback_info info) {
347
+ UseForegroundKeyboardLayoutScope use_foreground_keyboard_layout;
348
+
349
+ char chr_layout_name[KL_NAMELENGTH];
350
+ if (!GetKeyboardLayoutName(chr_layout_name)) {
351
+ return napi_fetch_null(env);
352
+ }
353
+ std::string layout_name = chr_layout_name;
354
+
355
+ // https://docs.microsoft.com/windows-hardware/manufacture/desktop/windows-language-pack-default-values
356
+ std::string layout_id = GetStringRegKey("System\\CurrentControlSet\\Control\\Keyboard Layouts\\" + layout_name, "Layout Id");
357
+ std::string layout_text = GetStringRegKey("System\\CurrentControlSet\\Control\\Keyboard Layouts\\" + layout_name, "Layout Text");
358
+
359
+ napi_value result;
360
+ NAPI_CALL(env, napi_create_object(env, &result));
361
+ NAPI_CALL(env, napi_set_named_property_string_utf8(env, result, "name", layout_name.c_str()));
362
+ NAPI_CALL(env, napi_set_named_property_string_utf8(env, result, "id", layout_id.c_str()));
363
+ NAPI_CALL(env, napi_set_named_property_string_utf8(env, result, "text", layout_text.c_str()));
364
+ return result;
365
+ }
366
+
367
+ class TfInputListener : public ITfInputProcessorProfileActivationSink {
368
+ private:
369
+ NotificationCallbackData *data_;
370
+ ULONG ref_count_;
371
+ ITfSource *source_;
372
+ DWORD cookie_;
373
+
374
+ public:
375
+ explicit TfInputListener(NotificationCallbackData *data) {
376
+ data_ = data;
377
+ ref_count_ = 1;
378
+ source_ = NULL;
379
+ cookie_ = TF_INVALID_COOKIE;
380
+ }
381
+
382
+ void StartListening() {
383
+ HRESULT hr;
384
+
385
+ ITfThreadMgr* thread_mgr;
386
+ hr = CoCreateInstance(CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, IID_ITfThreadMgr, (void**)&thread_mgr);
387
+ if (!SUCCEEDED(hr)) {
388
+ printf("native-keymap: Could not create ITfThreadMgr.\n");
389
+ return;
390
+ }
391
+
392
+ hr = thread_mgr->QueryInterface(IID_ITfSource, (LPVOID*)&source_);
393
+ if (!SUCCEEDED(hr)) {
394
+ printf("native-keymap: Could not obtain ITfSource.\n");
395
+ thread_mgr->Release();
396
+ return;
397
+ }
398
+
399
+ hr = source_->AdviseSink(IID_ITfInputProcessorProfileActivationSink,
400
+ static_cast<ITfInputProcessorProfileActivationSink*>(this),
401
+ &cookie_);
402
+
403
+ if (!SUCCEEDED(hr)) {
404
+ printf("native-keymap: Could not register ITfInputProcessorProfileActivationSink.\n");
405
+ }
406
+ thread_mgr->Release();
407
+ }
408
+
409
+ void StopListening() {
410
+ if (source_ != NULL) {
411
+ if (cookie_ != TF_INVALID_COOKIE) {
412
+ source_->UnadviseSink(cookie_);
413
+ cookie_ = TF_INVALID_COOKIE;
414
+ }
415
+ source_->Release();
416
+ source_ = NULL;
417
+ }
418
+ }
419
+
420
+ virtual ~TfInputListener() {
421
+ this->StopListening();
422
+ }
423
+
424
+ virtual HRESULT STDMETHODCALLTYPE OnActivated(
425
+ /* [in] */ DWORD dwProfileType,
426
+ /* [in] */ LANGID langid,
427
+ /* [in] */ __RPC__in REFCLSID clsid,
428
+ /* [in] */ __RPC__in REFGUID catid,
429
+ /* [in] */ __RPC__in REFGUID guidProfile,
430
+ /* [in] */ HKL hkl,
431
+ /* [in] */ DWORD dwFlags) override {
432
+
433
+ InvokeNotificationCallback(data_);
434
+ return S_OK;
435
+ }
436
+
437
+ // IUnknown methods
438
+ ULONG STDMETHODCALLTYPE AddRef() override {
439
+ return InterlockedIncrement(&ref_count_);
440
+ }
441
+
442
+ ULONG STDMETHODCALLTYPE Release() override {
443
+ ULONG newCount = InterlockedDecrement(&ref_count_);
444
+ if (0 == newCount) {
445
+ delete this;
446
+ }
447
+ return newCount;
448
+ }
449
+
450
+ virtual HRESULT STDMETHODCALLTYPE QueryInterface(IID const& riid, void** ppvObject) override {
451
+ if (__uuidof(IUnknown) == riid || __uuidof(ITfInputProcessorProfileActivationSink) == riid) {
452
+ *ppvObject = this;
453
+ this->AddRef();
454
+ return S_OK;
455
+ }
456
+ *ppvObject = nullptr;
457
+ return E_FAIL;
458
+ }
459
+ };
460
+
461
+ void RegisterKeyboardLayoutChangeListenerImpl(NotificationCallbackData *data) {
462
+ TfInputListener* listener = new TfInputListener(data);
463
+ listener->StartListening();
464
+ data->listener = listener;
465
+ }
466
+
467
+ void DisposeKeyboardLayoutChangeListenerImpl(NotificationCallbackData *data) {
468
+ TfInputListener* listener = static_cast<TfInputListener*>(data->listener);
469
+ listener->Release();
470
+ }
471
+
472
+ napi_value IsISOKeyboardImpl(napi_env env, napi_callback_info info) {
473
+ return napi_fetch_undefined(env);
474
+ }
475
+
476
+ } // namespace vscode_keyboard