keyboardEvent.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. import * as browser from './browser.js';
  6. import { EVENT_KEY_CODE_MAP, KeyCodeUtils } from '../common/keyCodes.js';
  7. import { KeyCodeChord } from '../common/keybindings.js';
  8. import * as platform from '../common/platform.js';
  9. function extractKeyCode(e) {
  10. if (e.charCode) {
  11. // "keypress" events mostly
  12. const char = String.fromCharCode(e.charCode).toUpperCase();
  13. return KeyCodeUtils.fromString(char);
  14. }
  15. const keyCode = e.keyCode;
  16. // browser quirks
  17. if (keyCode === 3) {
  18. return 7 /* KeyCode.PauseBreak */;
  19. }
  20. else if (browser.isFirefox) {
  21. switch (keyCode) {
  22. case 59: return 85 /* KeyCode.Semicolon */;
  23. case 60:
  24. if (platform.isLinux) {
  25. return 97 /* KeyCode.IntlBackslash */;
  26. }
  27. break;
  28. case 61: return 86 /* KeyCode.Equal */;
  29. // based on: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode#numpad_keys
  30. case 107: return 109 /* KeyCode.NumpadAdd */;
  31. case 109: return 111 /* KeyCode.NumpadSubtract */;
  32. case 173: return 88 /* KeyCode.Minus */;
  33. case 224:
  34. if (platform.isMacintosh) {
  35. return 57 /* KeyCode.Meta */;
  36. }
  37. break;
  38. }
  39. }
  40. else if (browser.isWebKit) {
  41. if (platform.isMacintosh && keyCode === 93) {
  42. // the two meta keys in the Mac have different key codes (91 and 93)
  43. return 57 /* KeyCode.Meta */;
  44. }
  45. else if (!platform.isMacintosh && keyCode === 92) {
  46. return 57 /* KeyCode.Meta */;
  47. }
  48. }
  49. // cross browser keycodes:
  50. return EVENT_KEY_CODE_MAP[keyCode] || 0 /* KeyCode.Unknown */;
  51. }
  52. const ctrlKeyMod = (platform.isMacintosh ? 256 /* KeyMod.WinCtrl */ : 2048 /* KeyMod.CtrlCmd */);
  53. const altKeyMod = 512 /* KeyMod.Alt */;
  54. const shiftKeyMod = 1024 /* KeyMod.Shift */;
  55. const metaKeyMod = (platform.isMacintosh ? 2048 /* KeyMod.CtrlCmd */ : 256 /* KeyMod.WinCtrl */);
  56. export class StandardKeyboardEvent {
  57. constructor(source) {
  58. this._standardKeyboardEventBrand = true;
  59. const e = source;
  60. this.browserEvent = e;
  61. this.target = e.target;
  62. this.ctrlKey = e.ctrlKey;
  63. this.shiftKey = e.shiftKey;
  64. this.altKey = e.altKey;
  65. this.metaKey = e.metaKey;
  66. this.altGraphKey = e.getModifierState('AltGraph');
  67. this.keyCode = extractKeyCode(e);
  68. this.code = e.code;
  69. // console.info(e.type + ": keyCode: " + e.keyCode + ", which: " + e.which + ", charCode: " + e.charCode + ", detail: " + e.detail + " ====> " + this.keyCode + ' -- ' + KeyCode[this.keyCode]);
  70. this.ctrlKey = this.ctrlKey || this.keyCode === 5 /* KeyCode.Ctrl */;
  71. this.altKey = this.altKey || this.keyCode === 6 /* KeyCode.Alt */;
  72. this.shiftKey = this.shiftKey || this.keyCode === 4 /* KeyCode.Shift */;
  73. this.metaKey = this.metaKey || this.keyCode === 57 /* KeyCode.Meta */;
  74. this._asKeybinding = this._computeKeybinding();
  75. this._asKeyCodeChord = this._computeKeyCodeChord();
  76. // console.log(`code: ${e.code}, keyCode: ${e.keyCode}, key: ${e.key}`);
  77. }
  78. preventDefault() {
  79. if (this.browserEvent && this.browserEvent.preventDefault) {
  80. this.browserEvent.preventDefault();
  81. }
  82. }
  83. stopPropagation() {
  84. if (this.browserEvent && this.browserEvent.stopPropagation) {
  85. this.browserEvent.stopPropagation();
  86. }
  87. }
  88. toKeyCodeChord() {
  89. return this._asKeyCodeChord;
  90. }
  91. equals(other) {
  92. return this._asKeybinding === other;
  93. }
  94. _computeKeybinding() {
  95. let key = 0 /* KeyCode.Unknown */;
  96. if (this.keyCode !== 5 /* KeyCode.Ctrl */ && this.keyCode !== 4 /* KeyCode.Shift */ && this.keyCode !== 6 /* KeyCode.Alt */ && this.keyCode !== 57 /* KeyCode.Meta */) {
  97. key = this.keyCode;
  98. }
  99. let result = 0;
  100. if (this.ctrlKey) {
  101. result |= ctrlKeyMod;
  102. }
  103. if (this.altKey) {
  104. result |= altKeyMod;
  105. }
  106. if (this.shiftKey) {
  107. result |= shiftKeyMod;
  108. }
  109. if (this.metaKey) {
  110. result |= metaKeyMod;
  111. }
  112. result |= key;
  113. return result;
  114. }
  115. _computeKeyCodeChord() {
  116. let key = 0 /* KeyCode.Unknown */;
  117. if (this.keyCode !== 5 /* KeyCode.Ctrl */ && this.keyCode !== 4 /* KeyCode.Shift */ && this.keyCode !== 6 /* KeyCode.Alt */ && this.keyCode !== 57 /* KeyCode.Meta */) {
  118. key = this.keyCode;
  119. }
  120. return new KeyCodeChord(this.ctrlKey, this.shiftKey, this.altKey, this.metaKey, key);
  121. }
  122. }