| 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 |
/** |
| 7 |
* Virtual Key Codes, the value does not hold any inherent meaning. |
| 8 |
* Inspired somewhat from https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx |
| 9 |
* But these are "more general", as they should work across browsers & OS`s. |
| 10 |
*/ |
| 11 |
export const enum KeyCode { |
| 12 |
DependsOnKbLayout = -1, |
| 13 |
|
| 14 |
/** |
| 15 |
* Placed first to cover the 0 value of the enum. |
| 16 |
*/ |
| 17 |
Unknown = 0, |
| 18 |
|
| 19 |
Backspace, |
| 20 |
Tab, |
| 21 |
Enter, |
| 22 |
Shift, |
| 23 |
Ctrl, |
| 24 |
Alt, |
| 25 |
PauseBreak, |
| 26 |
CapsLock, |
| 27 |
Escape, |
| 28 |
Space, |
| 29 |
PageUp, |
| 30 |
PageDown, |
| 31 |
End, |
| 32 |
Home, |
| 33 |
LeftArrow, |
| 34 |
UpArrow, |
| 35 |
RightArrow, |
| 36 |
DownArrow, |
| 37 |
Insert, |
| 38 |
Delete, |
| 39 |
|
| 40 |
Digit0, |
| 41 |
Digit1, |
| 42 |
Digit2, |
| 43 |
Digit3, |
| 44 |
Digit4, |
| 45 |
Digit5, |
| 46 |
Digit6, |
| 47 |
Digit7, |
| 48 |
Digit8, |
| 49 |
Digit9, |
| 50 |
|
| 51 |
KeyA, |
| 52 |
KeyB, |
| 53 |
KeyC, |
| 54 |
KeyD, |
| 55 |
KeyE, |
| 56 |
KeyF, |
| 57 |
KeyG, |
| 58 |
KeyH, |
| 59 |
KeyI, |
| 60 |
KeyJ, |
| 61 |
KeyK, |
| 62 |
KeyL, |
| 63 |
KeyM, |
| 64 |
KeyN, |
| 65 |
KeyO, |
| 66 |
KeyP, |
| 67 |
KeyQ, |
| 68 |
KeyR, |
| 69 |
KeyS, |
| 70 |
KeyT, |
| 71 |
KeyU, |
| 72 |
KeyV, |
| 73 |
KeyW, |
| 74 |
KeyX, |
| 75 |
KeyY, |
| 76 |
KeyZ, |
| 77 |
|
| 78 |
Meta, |
| 79 |
ContextMenu, |
| 80 |
|
| 81 |
F1, |
| 82 |
F2, |
| 83 |
F3, |
| 84 |
F4, |
| 85 |
F5, |
| 86 |
F6, |
| 87 |
F7, |
| 88 |
F8, |
| 89 |
F9, |
| 90 |
F10, |
| 91 |
F11, |
| 92 |
F12, |
| 93 |
F13, |
| 94 |
F14, |
| 95 |
F15, |
| 96 |
F16, |
| 97 |
F17, |
| 98 |
F18, |
| 99 |
F19, |
| 100 |
F20, |
| 101 |
F21, |
| 102 |
F22, |
| 103 |
F23, |
| 104 |
F24, |
| 105 |
|
| 106 |
NumLock, |
| 107 |
ScrollLock, |
| 108 |
|
| 109 |
/** |
| 110 |
* Used for miscellaneous characters; it can vary by keyboard. |
| 111 |
* For the US standard keyboard, the ';:' key |
| 112 |
*/ |
| 113 |
Semicolon, |
| 114 |
/** |
| 115 |
* For any country/region, the '+' key |
| 116 |
* For the US standard keyboard, the '=+' key |
| 117 |
*/ |
| 118 |
Equal, |
| 119 |
/** |
| 120 |
* For any country/region, the ',' key |
| 121 |
* For the US standard keyboard, the ',<' key |
| 122 |
*/ |
| 123 |
Comma, |
| 124 |
/** |
| 125 |
* For any country/region, the '-' key |
| 126 |
* For the US standard keyboard, the '-_' key |
| 127 |
*/ |
| 128 |
Minus, |
| 129 |
/** |
| 130 |
* For any country/region, the '.' key |
| 131 |
* For the US standard keyboard, the '.>' key |
| 132 |
*/ |
| 133 |
Period, |
| 134 |
/** |
| 135 |
* Used for miscellaneous characters; it can vary by keyboard. |
| 136 |
* For the US standard keyboard, the '/?' key |
| 137 |
*/ |
| 138 |
Slash, |
| 139 |
/** |
| 140 |
* Used for miscellaneous characters; it can vary by keyboard. |
| 141 |
* For the US standard keyboard, the '`~' key |
| 142 |
*/ |
| 143 |
Backquote, |
| 144 |
/** |
| 145 |
* Used for miscellaneous characters; it can vary by keyboard. |
| 146 |
* For the US standard keyboard, the '[{' key |
| 147 |
*/ |
| 148 |
BracketLeft, |
| 149 |
/** |
| 150 |
* Used for miscellaneous characters; it can vary by keyboard. |
| 151 |
* For the US standard keyboard, the '\|' key |
| 152 |
*/ |
| 153 |
Backslash, |
| 154 |
/** |
| 155 |
* Used for miscellaneous characters; it can vary by keyboard. |
| 156 |
* For the US standard keyboard, the ']}' key |
| 157 |
*/ |
| 158 |
BracketRight, |
| 159 |
/** |
| 160 |
* Used for miscellaneous characters; it can vary by keyboard. |
| 161 |
* For the US standard keyboard, the ''"' key |
| 162 |
*/ |
| 163 |
Quote, |
| 164 |
/** |
| 165 |
* Used for miscellaneous characters; it can vary by keyboard. |
| 166 |
*/ |
| 167 |
OEM_8, |
| 168 |
/** |
| 169 |
* Either the angle bracket key or the backslash key on the RT 102-key keyboard. |
| 170 |
*/ |
| 171 |
IntlBackslash, |
| 172 |
|
| 173 |
Numpad0, // VK_NUMPAD0, 0x60, Numeric keypad 0 key |
| 174 |
Numpad1, // VK_NUMPAD1, 0x61, Numeric keypad 1 key |
| 175 |
Numpad2, // VK_NUMPAD2, 0x62, Numeric keypad 2 key |
| 176 |
Numpad3, // VK_NUMPAD3, 0x63, Numeric keypad 3 key |
| 177 |
Numpad4, // VK_NUMPAD4, 0x64, Numeric keypad 4 key |
| 178 |
Numpad5, // VK_NUMPAD5, 0x65, Numeric keypad 5 key |
| 179 |
Numpad6, // VK_NUMPAD6, 0x66, Numeric keypad 6 key |
| 180 |
Numpad7, // VK_NUMPAD7, 0x67, Numeric keypad 7 key |
| 181 |
Numpad8, // VK_NUMPAD8, 0x68, Numeric keypad 8 key |
| 182 |
Numpad9, // VK_NUMPAD9, 0x69, Numeric keypad 9 key |
| 183 |
|
| 184 |
NumpadMultiply, // VK_MULTIPLY, 0x6A, Multiply key |
| 185 |
NumpadAdd, // VK_ADD, 0x6B, Add key |
| 186 |
NUMPAD_SEPARATOR, // VK_SEPARATOR, 0x6C, Separator key |
| 187 |
NumpadSubtract, // VK_SUBTRACT, 0x6D, Subtract key |
| 188 |
NumpadDecimal, // VK_DECIMAL, 0x6E, Decimal key |
| 189 |
NumpadDivide, // VK_DIVIDE, 0x6F, |
| 190 |
|
| 191 |
/** |
| 192 |
* Cover all key codes when IME is processing input. |
| 193 |
*/ |
| 194 |
KEY_IN_COMPOSITION, |
| 195 |
|
| 196 |
ABNT_C1, // Brazilian (ABNT) Keyboard |
| 197 |
ABNT_C2, // Brazilian (ABNT) Keyboard |
| 198 |
|
| 199 |
AudioVolumeMute, |
| 200 |
AudioVolumeUp, |
| 201 |
AudioVolumeDown, |
| 202 |
|
| 203 |
BrowserSearch, |
| 204 |
BrowserHome, |
| 205 |
BrowserBack, |
| 206 |
BrowserForward, |
| 207 |
|
| 208 |
MediaTrackNext, |
| 209 |
MediaTrackPrevious, |
| 210 |
MediaStop, |
| 211 |
MediaPlayPause, |
| 212 |
LaunchMediaPlayer, |
| 213 |
LaunchMail, |
| 214 |
LaunchApp2, |
| 215 |
|
| 216 |
/** |
| 217 |
* VK_CLEAR, 0x0C, CLEAR key |
| 218 |
*/ |
| 219 |
Clear, |
| 220 |
|
| 221 |
/** |
| 222 |
* Placed last to cover the length of the enum. |
| 223 |
* Please do not depend on this value! |
| 224 |
*/ |
| 225 |
MAX_VALUE |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* keyboardEvent.code |
| 230 |
*/ |
| 231 |
export const enum ScanCode { |
| 232 |
DependsOnKbLayout = -1, |
| 233 |
None, |
| 234 |
Hyper, |
| 235 |
Super, |
| 236 |
Fn, |
| 237 |
FnLock, |
| 238 |
Suspend, |
| 239 |
Resume, |
| 240 |
Turbo, |
| 241 |
Sleep, |
| 242 |
WakeUp, |
| 243 |
KeyA, |
| 244 |
KeyB, |
| 245 |
KeyC, |
| 246 |
KeyD, |
| 247 |
KeyE, |
| 248 |
KeyF, |
| 249 |
KeyG, |
| 250 |
KeyH, |
| 251 |
KeyI, |
| 252 |
KeyJ, |
| 253 |
KeyK, |
| 254 |
KeyL, |
| 255 |
KeyM, |
| 256 |
KeyN, |
| 257 |
KeyO, |
| 258 |
KeyP, |
| 259 |
KeyQ, |
| 260 |
KeyR, |
| 261 |
KeyS, |
| 262 |
KeyT, |
| 263 |
KeyU, |
| 264 |
KeyV, |
| 265 |
KeyW, |
| 266 |
KeyX, |
| 267 |
KeyY, |
| 268 |
KeyZ, |
| 269 |
Digit1, |
| 270 |
Digit2, |
| 271 |
Digit3, |
| 272 |
Digit4, |
| 273 |
Digit5, |
| 274 |
Digit6, |
| 275 |
Digit7, |
| 276 |
Digit8, |
| 277 |
Digit9, |
| 278 |
Digit0, |
| 279 |
Enter, |
| 280 |
Escape, |
| 281 |
Backspace, |
| 282 |
Tab, |
| 283 |
Space, |
| 284 |
Minus, |
| 285 |
Equal, |
| 286 |
BracketLeft, |
| 287 |
BracketRight, |
| 288 |
Backslash, |
| 289 |
IntlHash, |
| 290 |
Semicolon, |
| 291 |
Quote, |
| 292 |
Backquote, |
| 293 |
Comma, |
| 294 |
Period, |
| 295 |
Slash, |
| 296 |
CapsLock, |
| 297 |
F1, |
| 298 |
F2, |
| 299 |
F3, |
| 300 |
F4, |
| 301 |
F5, |
| 302 |
F6, |
| 303 |
F7, |
| 304 |
F8, |
| 305 |
F9, |
| 306 |
F10, |
| 307 |
F11, |
| 308 |
F12, |
| 309 |
PrintScreen, |
| 310 |
ScrollLock, |
| 311 |
Pause, |
| 312 |
Insert, |
| 313 |
Home, |
| 314 |
PageUp, |
| 315 |
Delete, |
| 316 |
End, |
| 317 |
PageDown, |
| 318 |
ArrowRight, |
| 319 |
ArrowLeft, |
| 320 |
ArrowDown, |
| 321 |
ArrowUp, |
| 322 |
NumLock, |
| 323 |
NumpadDivide, |
| 324 |
NumpadMultiply, |
| 325 |
NumpadSubtract, |
| 326 |
NumpadAdd, |
| 327 |
NumpadEnter, |
| 328 |
Numpad1, |
| 329 |
Numpad2, |
| 330 |
Numpad3, |
| 331 |
Numpad4, |
| 332 |
Numpad5, |
| 333 |
Numpad6, |
| 334 |
Numpad7, |
| 335 |
Numpad8, |
| 336 |
Numpad9, |
| 337 |
Numpad0, |
| 338 |
NumpadDecimal, |
| 339 |
IntlBackslash, |
| 340 |
ContextMenu, |
| 341 |
Power, |
| 342 |
NumpadEqual, |
| 343 |
F13, |
| 344 |
F14, |
| 345 |
F15, |
| 346 |
F16, |
| 347 |
F17, |
| 348 |
F18, |
| 349 |
F19, |
| 350 |
F20, |
| 351 |
F21, |
| 352 |
F22, |
| 353 |
F23, |
| 354 |
F24, |
| 355 |
Open, |
| 356 |
Help, |
| 357 |
Select, |
| 358 |
Again, |
| 359 |
Undo, |
| 360 |
Cut, |
| 361 |
Copy, |
| 362 |
Paste, |
| 363 |
Find, |
| 364 |
AudioVolumeMute, |
| 365 |
AudioVolumeUp, |
| 366 |
AudioVolumeDown, |
| 367 |
NumpadComma, |
| 368 |
IntlRo, |
| 369 |
KanaMode, |
| 370 |
IntlYen, |
| 371 |
Convert, |
| 372 |
NonConvert, |
| 373 |
Lang1, |
| 374 |
Lang2, |
| 375 |
Lang3, |
| 376 |
Lang4, |
| 377 |
Lang5, |
| 378 |
Abort, |
| 379 |
Props, |
| 380 |
NumpadParenLeft, |
| 381 |
NumpadParenRight, |
| 382 |
NumpadBackspace, |
| 383 |
NumpadMemoryStore, |
| 384 |
NumpadMemoryRecall, |
| 385 |
NumpadMemoryClear, |
| 386 |
NumpadMemoryAdd, |
| 387 |
NumpadMemorySubtract, |
| 388 |
NumpadClear, |
| 389 |
NumpadClearEntry, |
| 390 |
ControlLeft, |
| 391 |
ShiftLeft, |
| 392 |
AltLeft, |
| 393 |
MetaLeft, |
| 394 |
ControlRight, |
| 395 |
ShiftRight, |
| 396 |
AltRight, |
| 397 |
MetaRight, |
| 398 |
BrightnessUp, |
| 399 |
BrightnessDown, |
| 400 |
MediaPlay, |
| 401 |
MediaRecord, |
| 402 |
MediaFastForward, |
| 403 |
MediaRewind, |
| 404 |
MediaTrackNext, |
| 405 |
MediaTrackPrevious, |
| 406 |
MediaStop, |
| 407 |
Eject, |
| 408 |
MediaPlayPause, |
| 409 |
MediaSelect, |
| 410 |
LaunchMail, |
| 411 |
LaunchApp2, |
| 412 |
LaunchApp1, |
| 413 |
SelectTask, |
| 414 |
LaunchScreenSaver, |
| 415 |
BrowserSearch, |
| 416 |
BrowserHome, |
| 417 |
BrowserBack, |
| 418 |
BrowserForward, |
| 419 |
BrowserStop, |
| 420 |
BrowserRefresh, |
| 421 |
BrowserFavorites, |
| 422 |
ZoomToggle, |
| 423 |
MailReply, |
| 424 |
MailForward, |
| 425 |
MailSend, |
| 426 |
|
| 427 |
MAX_VALUE |
| 428 |
} |
| 429 |
|
| 430 |
class KeyCodeStrMap { |
| 431 |
|
| 432 |
public _keyCodeToStr: string[]; |
| 433 |
public _strToKeyCode: { [str: string]: KeyCode }; |
| 434 |
|
| 435 |
constructor() { |
| 436 |
this._keyCodeToStr = []; |
| 437 |
this._strToKeyCode = Object.create(null); |
| 438 |
} |
| 439 |
|
| 440 |
define(keyCode: KeyCode, str: string): void { |
| 441 |
this._keyCodeToStr[keyCode] = str; |
| 442 |
this._strToKeyCode[str.toLowerCase()] = keyCode; |
| 443 |
} |
| 444 |
|
| 445 |
keyCodeToStr(keyCode: KeyCode): string { |
| 446 |
return this._keyCodeToStr[keyCode]; |
| 447 |
} |
| 448 |
|
| 449 |
strToKeyCode(str: string): KeyCode { |
| 450 |
return this._strToKeyCode[str.toLowerCase()] || KeyCode.Unknown; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
const uiMap = new KeyCodeStrMap(); |
| 455 |
const userSettingsUSMap = new KeyCodeStrMap(); |
| 456 |
const userSettingsGeneralMap = new KeyCodeStrMap(); |
| 457 |
export const EVENT_KEY_CODE_MAP: { [keyCode: number]: KeyCode } = new Array(230); |
| 458 |
export const NATIVE_WINDOWS_KEY_CODE_TO_KEY_CODE: { [nativeKeyCode: string]: KeyCode } = {}; |
| 459 |
const scanCodeIntToStr: string[] = []; |
| 460 |
const scanCodeStrToInt: { [code: string]: number } = Object.create(null); |
| 461 |
const scanCodeLowerCaseStrToInt: { [code: string]: number } = Object.create(null); |
| 462 |
|
| 463 |
export const ScanCodeUtils = { |
| 464 |
lowerCaseToEnum: (scanCode: string) => scanCodeLowerCaseStrToInt[scanCode] || ScanCode.None, |
| 465 |
toEnum: (scanCode: string) => scanCodeStrToInt[scanCode] || ScanCode.None, |
| 466 |
toString: (scanCode: ScanCode) => scanCodeIntToStr[scanCode] || 'None' |
| 467 |
}; |
| 468 |
|
| 469 |
|
| 470 |
export namespace KeyCodeUtils { |
| 471 |
export function toString(keyCode: KeyCode): string { |
| 472 |
return uiMap.keyCodeToStr(keyCode); |
| 473 |
} |
| 474 |
export function fromString(key: string): KeyCode { |
| 475 |
return uiMap.strToKeyCode(key); |
| 476 |
} |
| 477 |
|
| 478 |
export function toUserSettingsUS(keyCode: KeyCode): string { |
| 479 |
return userSettingsUSMap.keyCodeToStr(keyCode); |
| 480 |
} |
| 481 |
export function toUserSettingsGeneral(keyCode: KeyCode): string { |
| 482 |
return userSettingsGeneralMap.keyCodeToStr(keyCode); |
| 483 |
} |
| 484 |
export function fromUserSettings(key: string): KeyCode { |
| 485 |
return userSettingsUSMap.strToKeyCode(key) || userSettingsGeneralMap.strToKeyCode(key); |
| 486 |
} |
| 487 |
|
| 488 |
export function toElectronAccelerator(keyCode: KeyCode): string | null { |
| 489 |
if (keyCode >= KeyCode.Numpad0 && keyCode <= KeyCode.NumpadDivide) { |
| 490 |
// [Electron Accelerators] Electron is able to parse numpad keys, but unfortunately it |
| 491 |
// renders them just as regular keys in menus. For example, num0 is rendered as "0", |
| 492 |
// numdiv is rendered as "/", numsub is rendered as "-". |
| 493 |
// |
| 494 |
// This can lead to incredible confusion, as it makes numpad based keybindings indistinguishable |
| 495 |
// from keybindings based on regular keys. |
| 496 |
// |
| 497 |
// We therefore need to fall back to custom rendering for numpad keys. |
| 498 |
return null; |
| 499 |
} |
| 500 |
|
| 501 |
switch (keyCode) { |
| 502 |
case KeyCode.UpArrow: |
| 503 |
return 'Up'; |
| 504 |
case KeyCode.DownArrow: |
| 505 |
return 'Down'; |
| 506 |
case KeyCode.LeftArrow: |
| 507 |
return 'Left'; |
| 508 |
case KeyCode.RightArrow: |
| 509 |
return 'Right'; |
| 510 |
} |
| 511 |
|
| 512 |
return uiMap.keyCodeToStr(keyCode); |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
export const enum KeyMod { |
| 517 |
CtrlCmd = (1 << 11) >>> 0, |
| 518 |
Shift = (1 << 10) >>> 0, |
| 519 |
Alt = (1 << 9) >>> 0, |
| 520 |
WinCtrl = (1 << 8) >>> 0, |
| 521 |
} |
| 522 |
|
| 523 |
export function KeyChord(firstPart: number, secondPart: number): number { |
| 524 |
const chordPart = ((secondPart & 0x0000FFFF) << 16) >>> 0; |
| 525 |
return (firstPart | chordPart) >>> 0; |
| 526 |
} |