keybindings.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 createKeybinding(keybinding, OS) {
  7. if (keybinding === 0) {
  8. return null;
  9. }
  10. const firstPart = (keybinding & 0x0000FFFF) >>> 0;
  11. const chordPart = (keybinding & 0xFFFF0000) >>> 16;
  12. if (chordPart !== 0) {
  13. return new ChordKeybinding([
  14. createSimpleKeybinding(firstPart, OS),
  15. createSimpleKeybinding(chordPart, OS)
  16. ]);
  17. }
  18. return new ChordKeybinding([createSimpleKeybinding(firstPart, OS)]);
  19. }
  20. export function createSimpleKeybinding(keybinding, OS) {
  21. const ctrlCmd = (keybinding & 2048 /* CtrlCmd */ ? true : false);
  22. const winCtrl = (keybinding & 256 /* WinCtrl */ ? true : false);
  23. const ctrlKey = (OS === 2 /* Macintosh */ ? winCtrl : ctrlCmd);
  24. const shiftKey = (keybinding & 1024 /* Shift */ ? true : false);
  25. const altKey = (keybinding & 512 /* Alt */ ? true : false);
  26. const metaKey = (OS === 2 /* Macintosh */ ? ctrlCmd : winCtrl);
  27. const keyCode = (keybinding & 255 /* KeyCode */);
  28. return new SimpleKeybinding(ctrlKey, shiftKey, altKey, metaKey, keyCode);
  29. }
  30. export class SimpleKeybinding {
  31. constructor(ctrlKey, shiftKey, altKey, metaKey, keyCode) {
  32. this.ctrlKey = ctrlKey;
  33. this.shiftKey = shiftKey;
  34. this.altKey = altKey;
  35. this.metaKey = metaKey;
  36. this.keyCode = keyCode;
  37. }
  38. equals(other) {
  39. return (this.ctrlKey === other.ctrlKey
  40. && this.shiftKey === other.shiftKey
  41. && this.altKey === other.altKey
  42. && this.metaKey === other.metaKey
  43. && this.keyCode === other.keyCode);
  44. }
  45. isModifierKey() {
  46. return (this.keyCode === 0 /* Unknown */
  47. || this.keyCode === 5 /* Ctrl */
  48. || this.keyCode === 57 /* Meta */
  49. || this.keyCode === 6 /* Alt */
  50. || this.keyCode === 4 /* Shift */);
  51. }
  52. toChord() {
  53. return new ChordKeybinding([this]);
  54. }
  55. /**
  56. * Does this keybinding refer to the key code of a modifier and it also has the modifier flag?
  57. */
  58. isDuplicateModifierCase() {
  59. return ((this.ctrlKey && this.keyCode === 5 /* Ctrl */)
  60. || (this.shiftKey && this.keyCode === 4 /* Shift */)
  61. || (this.altKey && this.keyCode === 6 /* Alt */)
  62. || (this.metaKey && this.keyCode === 57 /* Meta */));
  63. }
  64. }
  65. export class ChordKeybinding {
  66. constructor(parts) {
  67. if (parts.length === 0) {
  68. throw illegalArgument(`parts`);
  69. }
  70. this.parts = parts;
  71. }
  72. }
  73. export class ScanCodeBinding {
  74. constructor(ctrlKey, shiftKey, altKey, metaKey, scanCode) {
  75. this.ctrlKey = ctrlKey;
  76. this.shiftKey = shiftKey;
  77. this.altKey = altKey;
  78. this.metaKey = metaKey;
  79. this.scanCode = scanCode;
  80. }
  81. /**
  82. * Does this keybinding refer to the key code of a modifier and it also has the modifier flag?
  83. */
  84. isDuplicateModifierCase() {
  85. return ((this.ctrlKey && (this.scanCode === 157 /* ControlLeft */ || this.scanCode === 161 /* ControlRight */))
  86. || (this.shiftKey && (this.scanCode === 158 /* ShiftLeft */ || this.scanCode === 162 /* ShiftRight */))
  87. || (this.altKey && (this.scanCode === 159 /* AltLeft */ || this.scanCode === 163 /* AltRight */))
  88. || (this.metaKey && (this.scanCode === 160 /* MetaLeft */ || this.scanCode === 164 /* MetaRight */)));
  89. }
  90. }
  91. export class ResolvedKeybindingPart {
  92. constructor(ctrlKey, shiftKey, altKey, metaKey, kbLabel, kbAriaLabel) {
  93. this.ctrlKey = ctrlKey;
  94. this.shiftKey = shiftKey;
  95. this.altKey = altKey;
  96. this.metaKey = metaKey;
  97. this.keyLabel = kbLabel;
  98. this.keyAriaLabel = kbAriaLabel;
  99. }
  100. }
  101. /**
  102. * A resolved keybinding. Can be a simple keybinding or a chord keybinding.
  103. */
  104. export class ResolvedKeybinding {
  105. }