keyboardEvent.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 { SimpleKeybinding } 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. let 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 /* PauseBreak */;
  19. }
  20. else if (browser.isFirefox) {
  21. if (keyCode === 59) {
  22. return 80 /* Semicolon */;
  23. }
  24. else if (keyCode === 107) {
  25. return 81 /* Equal */;
  26. }
  27. else if (keyCode === 109) {
  28. return 83 /* Minus */;
  29. }
  30. else if (platform.isMacintosh && keyCode === 224) {
  31. return 57 /* Meta */;
  32. }
  33. }
  34. else if (browser.isWebKit) {
  35. if (keyCode === 91) {
  36. return 57 /* Meta */;
  37. }
  38. else if (platform.isMacintosh && keyCode === 93) {
  39. // the two meta keys in the Mac have different key codes (91 and 93)
  40. return 57 /* Meta */;
  41. }
  42. else if (!platform.isMacintosh && keyCode === 92) {
  43. return 57 /* Meta */;
  44. }
  45. }
  46. // cross browser keycodes:
  47. return EVENT_KEY_CODE_MAP[keyCode] || 0 /* Unknown */;
  48. }
  49. const ctrlKeyMod = (platform.isMacintosh ? 256 /* WinCtrl */ : 2048 /* CtrlCmd */);
  50. const altKeyMod = 512 /* Alt */;
  51. const shiftKeyMod = 1024 /* Shift */;
  52. const metaKeyMod = (platform.isMacintosh ? 2048 /* CtrlCmd */ : 256 /* WinCtrl */);
  53. export class StandardKeyboardEvent {
  54. constructor(source) {
  55. this._standardKeyboardEventBrand = true;
  56. let e = source;
  57. this.browserEvent = e;
  58. this.target = e.target;
  59. this.ctrlKey = e.ctrlKey;
  60. this.shiftKey = e.shiftKey;
  61. this.altKey = e.altKey;
  62. this.metaKey = e.metaKey;
  63. this.keyCode = extractKeyCode(e);
  64. this.code = e.code;
  65. // console.info(e.type + ": keyCode: " + e.keyCode + ", which: " + e.which + ", charCode: " + e.charCode + ", detail: " + e.detail + " ====> " + this.keyCode + ' -- ' + KeyCode[this.keyCode]);
  66. this.ctrlKey = this.ctrlKey || this.keyCode === 5 /* Ctrl */;
  67. this.altKey = this.altKey || this.keyCode === 6 /* Alt */;
  68. this.shiftKey = this.shiftKey || this.keyCode === 4 /* Shift */;
  69. this.metaKey = this.metaKey || this.keyCode === 57 /* Meta */;
  70. this._asKeybinding = this._computeKeybinding();
  71. this._asRuntimeKeybinding = this._computeRuntimeKeybinding();
  72. // console.log(`code: ${e.code}, keyCode: ${e.keyCode}, key: ${e.key}`);
  73. }
  74. preventDefault() {
  75. if (this.browserEvent && this.browserEvent.preventDefault) {
  76. this.browserEvent.preventDefault();
  77. }
  78. }
  79. stopPropagation() {
  80. if (this.browserEvent && this.browserEvent.stopPropagation) {
  81. this.browserEvent.stopPropagation();
  82. }
  83. }
  84. toKeybinding() {
  85. return this._asRuntimeKeybinding;
  86. }
  87. equals(other) {
  88. return this._asKeybinding === other;
  89. }
  90. _computeKeybinding() {
  91. let key = 0 /* Unknown */;
  92. if (this.keyCode !== 5 /* Ctrl */ && this.keyCode !== 4 /* Shift */ && this.keyCode !== 6 /* Alt */ && this.keyCode !== 57 /* Meta */) {
  93. key = this.keyCode;
  94. }
  95. let result = 0;
  96. if (this.ctrlKey) {
  97. result |= ctrlKeyMod;
  98. }
  99. if (this.altKey) {
  100. result |= altKeyMod;
  101. }
  102. if (this.shiftKey) {
  103. result |= shiftKeyMod;
  104. }
  105. if (this.metaKey) {
  106. result |= metaKeyMod;
  107. }
  108. result |= key;
  109. return result;
  110. }
  111. _computeRuntimeKeybinding() {
  112. let key = 0 /* Unknown */;
  113. if (this.keyCode !== 5 /* Ctrl */ && this.keyCode !== 4 /* Shift */ && this.keyCode !== 6 /* Alt */ && this.keyCode !== 57 /* Meta */) {
  114. key = this.keyCode;
  115. }
  116. return new SimpleKeybinding(this.ctrlKey, this.shiftKey, this.altKey, this.metaKey, key);
  117. }
  118. }