usLayoutResolvedKeybinding.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 { KeyCodeUtils, IMMUTABLE_CODE_TO_KEY_CODE } from '../../../base/common/keyCodes.js';
  6. import { ChordKeybinding, SimpleKeybinding } from '../../../base/common/keybindings.js';
  7. import { BaseResolvedKeybinding } from './baseResolvedKeybinding.js';
  8. import { removeElementsAfterNulls } from './resolvedKeybindingItem.js';
  9. /**
  10. * Do not instantiate. Use KeybindingService to get a ResolvedKeybinding seeded with information about the current kb layout.
  11. */
  12. export class USLayoutResolvedKeybinding extends BaseResolvedKeybinding {
  13. constructor(actual, os) {
  14. super(os, actual.parts);
  15. }
  16. _keyCodeToUILabel(keyCode) {
  17. if (this._os === 2 /* Macintosh */) {
  18. switch (keyCode) {
  19. case 15 /* LeftArrow */:
  20. return '←';
  21. case 16 /* UpArrow */:
  22. return '↑';
  23. case 17 /* RightArrow */:
  24. return '→';
  25. case 18 /* DownArrow */:
  26. return '↓';
  27. }
  28. }
  29. return KeyCodeUtils.toString(keyCode);
  30. }
  31. _getLabel(keybinding) {
  32. if (keybinding.isDuplicateModifierCase()) {
  33. return '';
  34. }
  35. return this._keyCodeToUILabel(keybinding.keyCode);
  36. }
  37. _getAriaLabel(keybinding) {
  38. if (keybinding.isDuplicateModifierCase()) {
  39. return '';
  40. }
  41. return KeyCodeUtils.toString(keybinding.keyCode);
  42. }
  43. _getElectronAccelerator(keybinding) {
  44. return KeyCodeUtils.toElectronAccelerator(keybinding.keyCode);
  45. }
  46. _getDispatchPart(keybinding) {
  47. return USLayoutResolvedKeybinding.getDispatchStr(keybinding);
  48. }
  49. static getDispatchStr(keybinding) {
  50. if (keybinding.isModifierKey()) {
  51. return null;
  52. }
  53. let result = '';
  54. if (keybinding.ctrlKey) {
  55. result += 'ctrl+';
  56. }
  57. if (keybinding.shiftKey) {
  58. result += 'shift+';
  59. }
  60. if (keybinding.altKey) {
  61. result += 'alt+';
  62. }
  63. if (keybinding.metaKey) {
  64. result += 'meta+';
  65. }
  66. result += KeyCodeUtils.toString(keybinding.keyCode);
  67. return result;
  68. }
  69. _getSingleModifierDispatchPart(keybinding) {
  70. if (keybinding.keyCode === 5 /* Ctrl */ && !keybinding.shiftKey && !keybinding.altKey && !keybinding.metaKey) {
  71. return 'ctrl';
  72. }
  73. if (keybinding.keyCode === 4 /* Shift */ && !keybinding.ctrlKey && !keybinding.altKey && !keybinding.metaKey) {
  74. return 'shift';
  75. }
  76. if (keybinding.keyCode === 6 /* Alt */ && !keybinding.ctrlKey && !keybinding.shiftKey && !keybinding.metaKey) {
  77. return 'alt';
  78. }
  79. if (keybinding.keyCode === 57 /* Meta */ && !keybinding.ctrlKey && !keybinding.shiftKey && !keybinding.altKey) {
  80. return 'meta';
  81. }
  82. return null;
  83. }
  84. /**
  85. * *NOTE*: Check return value for `KeyCode.Unknown`.
  86. */
  87. static _scanCodeToKeyCode(scanCode) {
  88. const immutableKeyCode = IMMUTABLE_CODE_TO_KEY_CODE[scanCode];
  89. if (immutableKeyCode !== -1 /* DependsOnKbLayout */) {
  90. return immutableKeyCode;
  91. }
  92. switch (scanCode) {
  93. case 10 /* KeyA */: return 31 /* KeyA */;
  94. case 11 /* KeyB */: return 32 /* KeyB */;
  95. case 12 /* KeyC */: return 33 /* KeyC */;
  96. case 13 /* KeyD */: return 34 /* KeyD */;
  97. case 14 /* KeyE */: return 35 /* KeyE */;
  98. case 15 /* KeyF */: return 36 /* KeyF */;
  99. case 16 /* KeyG */: return 37 /* KeyG */;
  100. case 17 /* KeyH */: return 38 /* KeyH */;
  101. case 18 /* KeyI */: return 39 /* KeyI */;
  102. case 19 /* KeyJ */: return 40 /* KeyJ */;
  103. case 20 /* KeyK */: return 41 /* KeyK */;
  104. case 21 /* KeyL */: return 42 /* KeyL */;
  105. case 22 /* KeyM */: return 43 /* KeyM */;
  106. case 23 /* KeyN */: return 44 /* KeyN */;
  107. case 24 /* KeyO */: return 45 /* KeyO */;
  108. case 25 /* KeyP */: return 46 /* KeyP */;
  109. case 26 /* KeyQ */: return 47 /* KeyQ */;
  110. case 27 /* KeyR */: return 48 /* KeyR */;
  111. case 28 /* KeyS */: return 49 /* KeyS */;
  112. case 29 /* KeyT */: return 50 /* KeyT */;
  113. case 30 /* KeyU */: return 51 /* KeyU */;
  114. case 31 /* KeyV */: return 52 /* KeyV */;
  115. case 32 /* KeyW */: return 53 /* KeyW */;
  116. case 33 /* KeyX */: return 54 /* KeyX */;
  117. case 34 /* KeyY */: return 55 /* KeyY */;
  118. case 35 /* KeyZ */: return 56 /* KeyZ */;
  119. case 36 /* Digit1 */: return 22 /* Digit1 */;
  120. case 37 /* Digit2 */: return 23 /* Digit2 */;
  121. case 38 /* Digit3 */: return 24 /* Digit3 */;
  122. case 39 /* Digit4 */: return 25 /* Digit4 */;
  123. case 40 /* Digit5 */: return 26 /* Digit5 */;
  124. case 41 /* Digit6 */: return 27 /* Digit6 */;
  125. case 42 /* Digit7 */: return 28 /* Digit7 */;
  126. case 43 /* Digit8 */: return 29 /* Digit8 */;
  127. case 44 /* Digit9 */: return 30 /* Digit9 */;
  128. case 45 /* Digit0 */: return 21 /* Digit0 */;
  129. case 51 /* Minus */: return 83 /* Minus */;
  130. case 52 /* Equal */: return 81 /* Equal */;
  131. case 53 /* BracketLeft */: return 87 /* BracketLeft */;
  132. case 54 /* BracketRight */: return 89 /* BracketRight */;
  133. case 55 /* Backslash */: return 88 /* Backslash */;
  134. case 56 /* IntlHash */: return 0 /* Unknown */; // missing
  135. case 57 /* Semicolon */: return 80 /* Semicolon */;
  136. case 58 /* Quote */: return 90 /* Quote */;
  137. case 59 /* Backquote */: return 86 /* Backquote */;
  138. case 60 /* Comma */: return 82 /* Comma */;
  139. case 61 /* Period */: return 84 /* Period */;
  140. case 62 /* Slash */: return 85 /* Slash */;
  141. case 106 /* IntlBackslash */: return 92 /* IntlBackslash */;
  142. }
  143. return 0 /* Unknown */;
  144. }
  145. static _resolveSimpleUserBinding(binding) {
  146. if (!binding) {
  147. return null;
  148. }
  149. if (binding instanceof SimpleKeybinding) {
  150. return binding;
  151. }
  152. const keyCode = this._scanCodeToKeyCode(binding.scanCode);
  153. if (keyCode === 0 /* Unknown */) {
  154. return null;
  155. }
  156. return new SimpleKeybinding(binding.ctrlKey, binding.shiftKey, binding.altKey, binding.metaKey, keyCode);
  157. }
  158. static resolveUserBinding(input, os) {
  159. const parts = removeElementsAfterNulls(input.map(keybinding => this._resolveSimpleUserBinding(keybinding)));
  160. if (parts.length > 0) {
  161. return [new USLayoutResolvedKeybinding(new ChordKeybinding(parts), os)];
  162. }
  163. return [];
  164. }
  165. }