keybindings.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 { illegalArgument } from './errors.js';
  6. export function decodeKeybinding(keybinding, OS) {
  7. if (typeof keybinding === 'number') {
  8. if (keybinding === 0) {
  9. return null;
  10. }
  11. const firstChord = (keybinding & 0x0000FFFF) >>> 0;
  12. const secondChord = (keybinding & 0xFFFF0000) >>> 16;
  13. if (secondChord !== 0) {
  14. return new Keybinding([
  15. createSimpleKeybinding(firstChord, OS),
  16. createSimpleKeybinding(secondChord, OS)
  17. ]);
  18. }
  19. return new Keybinding([createSimpleKeybinding(firstChord, OS)]);
  20. }
  21. else {
  22. const chords = [];
  23. for (let i = 0; i < keybinding.length; i++) {
  24. chords.push(createSimpleKeybinding(keybinding[i], OS));
  25. }
  26. return new Keybinding(chords);
  27. }
  28. }
  29. export function createSimpleKeybinding(keybinding, OS) {
  30. const ctrlCmd = (keybinding & 2048 /* BinaryKeybindingsMask.CtrlCmd */ ? true : false);
  31. const winCtrl = (keybinding & 256 /* BinaryKeybindingsMask.WinCtrl */ ? true : false);
  32. const ctrlKey = (OS === 2 /* OperatingSystem.Macintosh */ ? winCtrl : ctrlCmd);
  33. const shiftKey = (keybinding & 1024 /* BinaryKeybindingsMask.Shift */ ? true : false);
  34. const altKey = (keybinding & 512 /* BinaryKeybindingsMask.Alt */ ? true : false);
  35. const metaKey = (OS === 2 /* OperatingSystem.Macintosh */ ? ctrlCmd : winCtrl);
  36. const keyCode = (keybinding & 255 /* BinaryKeybindingsMask.KeyCode */);
  37. return new KeyCodeChord(ctrlKey, shiftKey, altKey, metaKey, keyCode);
  38. }
  39. /**
  40. * Represents a chord which uses the `keyCode` field of keyboard events.
  41. * A chord is a combination of keys pressed simultaneously.
  42. */
  43. export class KeyCodeChord {
  44. constructor(ctrlKey, shiftKey, altKey, metaKey, keyCode) {
  45. this.ctrlKey = ctrlKey;
  46. this.shiftKey = shiftKey;
  47. this.altKey = altKey;
  48. this.metaKey = metaKey;
  49. this.keyCode = keyCode;
  50. }
  51. equals(other) {
  52. return (other instanceof KeyCodeChord
  53. && this.ctrlKey === other.ctrlKey
  54. && this.shiftKey === other.shiftKey
  55. && this.altKey === other.altKey
  56. && this.metaKey === other.metaKey
  57. && this.keyCode === other.keyCode);
  58. }
  59. isModifierKey() {
  60. return (this.keyCode === 0 /* KeyCode.Unknown */
  61. || this.keyCode === 5 /* KeyCode.Ctrl */
  62. || this.keyCode === 57 /* KeyCode.Meta */
  63. || this.keyCode === 6 /* KeyCode.Alt */
  64. || this.keyCode === 4 /* KeyCode.Shift */);
  65. }
  66. /**
  67. * Does this keybinding refer to the key code of a modifier and it also has the modifier flag?
  68. */
  69. isDuplicateModifierCase() {
  70. return ((this.ctrlKey && this.keyCode === 5 /* KeyCode.Ctrl */)
  71. || (this.shiftKey && this.keyCode === 4 /* KeyCode.Shift */)
  72. || (this.altKey && this.keyCode === 6 /* KeyCode.Alt */)
  73. || (this.metaKey && this.keyCode === 57 /* KeyCode.Meta */));
  74. }
  75. }
  76. /**
  77. * Represents a chord which uses the `code` field of keyboard events.
  78. * A chord is a combination of keys pressed simultaneously.
  79. */
  80. export class ScanCodeChord {
  81. constructor(ctrlKey, shiftKey, altKey, metaKey, scanCode) {
  82. this.ctrlKey = ctrlKey;
  83. this.shiftKey = shiftKey;
  84. this.altKey = altKey;
  85. this.metaKey = metaKey;
  86. this.scanCode = scanCode;
  87. }
  88. /**
  89. * Does this keybinding refer to the key code of a modifier and it also has the modifier flag?
  90. */
  91. isDuplicateModifierCase() {
  92. return ((this.ctrlKey && (this.scanCode === 157 /* ScanCode.ControlLeft */ || this.scanCode === 161 /* ScanCode.ControlRight */))
  93. || (this.shiftKey && (this.scanCode === 158 /* ScanCode.ShiftLeft */ || this.scanCode === 162 /* ScanCode.ShiftRight */))
  94. || (this.altKey && (this.scanCode === 159 /* ScanCode.AltLeft */ || this.scanCode === 163 /* ScanCode.AltRight */))
  95. || (this.metaKey && (this.scanCode === 160 /* ScanCode.MetaLeft */ || this.scanCode === 164 /* ScanCode.MetaRight */)));
  96. }
  97. }
  98. /**
  99. * A keybinding is a sequence of chords.
  100. */
  101. export class Keybinding {
  102. constructor(chords) {
  103. if (chords.length === 0) {
  104. throw illegalArgument(`chords`);
  105. }
  106. this.chords = chords;
  107. }
  108. }
  109. export class ResolvedChord {
  110. constructor(ctrlKey, shiftKey, altKey, metaKey, keyLabel, keyAriaLabel) {
  111. this.ctrlKey = ctrlKey;
  112. this.shiftKey = shiftKey;
  113. this.altKey = altKey;
  114. this.metaKey = metaKey;
  115. this.keyLabel = keyLabel;
  116. this.keyAriaLabel = keyAriaLabel;
  117. }
  118. }
  119. /**
  120. * A resolved keybinding. Consists of one or multiple chords.
  121. */
  122. export class ResolvedKeybinding {
  123. }