|
3 | 3 | * Licensed under the MIT License. See License.txt in the project root for license information.
|
4 | 4 | *--------------------------------------------------------------------------------------------*/
|
5 | 5 |
|
6 |
| -import { Keybinding, KeybindingModifier, KeyCode, KeyCodeUtils, SimpleKeybinding } from 'vs/base/common/keyCodes'; |
| 6 | +import { ChordKeybinding, Keybinding, KeybindingModifier, KeyCode, KeyCodeUtils, SimpleKeybinding } from 'vs/base/common/keyCodes'; |
7 | 7 | import { OperatingSystem } from 'vs/base/common/platform';
|
| 8 | +import { IMMUTABLE_CODE_TO_KEY_CODE, ScanCode, ScanCodeBinding } from 'vs/base/common/scanCode'; |
8 | 9 | import { BaseResolvedKeybinding } from 'vs/platform/keybinding/common/baseResolvedKeybinding';
|
| 10 | +import { removeElementsAfterNulls } from 'vs/platform/keybinding/common/resolvedKeybindingItem'; |
9 | 11 |
|
10 | 12 | /**
|
11 | 13 | * Do not instantiate. Use KeybindingService to get a ResolvedKeybinding seeded with information about the current kb layout.
|
@@ -104,4 +106,89 @@ export class USLayoutResolvedKeybinding extends BaseResolvedKeybinding<SimpleKey
|
104 | 106 | }
|
105 | 107 | return null;
|
106 | 108 | }
|
| 109 | + |
| 110 | + /** |
| 111 | + * *NOTE*: Check return value for `KeyCode.Unknown`. |
| 112 | + */ |
| 113 | + private static _scanCodeToKeyCode(scanCode: ScanCode): KeyCode { |
| 114 | + const immutableKeyCode = IMMUTABLE_CODE_TO_KEY_CODE[scanCode]; |
| 115 | + if (immutableKeyCode !== KeyCode.DependsOnKbLayout) { |
| 116 | + return immutableKeyCode; |
| 117 | + } |
| 118 | + |
| 119 | + switch (scanCode) { |
| 120 | + case ScanCode.KeyA: return KeyCode.KEY_A; |
| 121 | + case ScanCode.KeyB: return KeyCode.KEY_B; |
| 122 | + case ScanCode.KeyC: return KeyCode.KEY_C; |
| 123 | + case ScanCode.KeyD: return KeyCode.KEY_D; |
| 124 | + case ScanCode.KeyE: return KeyCode.KEY_E; |
| 125 | + case ScanCode.KeyF: return KeyCode.KEY_F; |
| 126 | + case ScanCode.KeyG: return KeyCode.KEY_G; |
| 127 | + case ScanCode.KeyH: return KeyCode.KEY_H; |
| 128 | + case ScanCode.KeyI: return KeyCode.KEY_I; |
| 129 | + case ScanCode.KeyJ: return KeyCode.KEY_J; |
| 130 | + case ScanCode.KeyK: return KeyCode.KEY_K; |
| 131 | + case ScanCode.KeyL: return KeyCode.KEY_L; |
| 132 | + case ScanCode.KeyM: return KeyCode.KEY_M; |
| 133 | + case ScanCode.KeyN: return KeyCode.KEY_N; |
| 134 | + case ScanCode.KeyO: return KeyCode.KEY_O; |
| 135 | + case ScanCode.KeyP: return KeyCode.KEY_P; |
| 136 | + case ScanCode.KeyQ: return KeyCode.KEY_Q; |
| 137 | + case ScanCode.KeyR: return KeyCode.KEY_R; |
| 138 | + case ScanCode.KeyS: return KeyCode.KEY_S; |
| 139 | + case ScanCode.KeyT: return KeyCode.KEY_T; |
| 140 | + case ScanCode.KeyU: return KeyCode.KEY_U; |
| 141 | + case ScanCode.KeyV: return KeyCode.KEY_V; |
| 142 | + case ScanCode.KeyW: return KeyCode.KEY_W; |
| 143 | + case ScanCode.KeyX: return KeyCode.KEY_X; |
| 144 | + case ScanCode.KeyY: return KeyCode.KEY_Y; |
| 145 | + case ScanCode.KeyZ: return KeyCode.KEY_Z; |
| 146 | + case ScanCode.Digit1: return KeyCode.KEY_1; |
| 147 | + case ScanCode.Digit2: return KeyCode.KEY_2; |
| 148 | + case ScanCode.Digit3: return KeyCode.KEY_3; |
| 149 | + case ScanCode.Digit4: return KeyCode.KEY_4; |
| 150 | + case ScanCode.Digit5: return KeyCode.KEY_5; |
| 151 | + case ScanCode.Digit6: return KeyCode.KEY_6; |
| 152 | + case ScanCode.Digit7: return KeyCode.KEY_7; |
| 153 | + case ScanCode.Digit8: return KeyCode.KEY_8; |
| 154 | + case ScanCode.Digit9: return KeyCode.KEY_9; |
| 155 | + case ScanCode.Digit0: return KeyCode.KEY_0; |
| 156 | + case ScanCode.Minus: return KeyCode.US_MINUS; |
| 157 | + case ScanCode.Equal: return KeyCode.US_EQUAL; |
| 158 | + case ScanCode.BracketLeft: return KeyCode.US_OPEN_SQUARE_BRACKET; |
| 159 | + case ScanCode.BracketRight: return KeyCode.US_CLOSE_SQUARE_BRACKET; |
| 160 | + case ScanCode.Backslash: return KeyCode.US_BACKSLASH; |
| 161 | + case ScanCode.IntlHash: return KeyCode.Unknown; // missing |
| 162 | + case ScanCode.Semicolon: return KeyCode.US_SEMICOLON; |
| 163 | + case ScanCode.Quote: return KeyCode.US_QUOTE; |
| 164 | + case ScanCode.Backquote: return KeyCode.US_BACKTICK; |
| 165 | + case ScanCode.Comma: return KeyCode.US_COMMA; |
| 166 | + case ScanCode.Period: return KeyCode.US_DOT; |
| 167 | + case ScanCode.Slash: return KeyCode.US_SLASH; |
| 168 | + case ScanCode.IntlBackslash: return KeyCode.OEM_102; |
| 169 | + } |
| 170 | + return KeyCode.Unknown; |
| 171 | + } |
| 172 | + |
| 173 | + private static _resolveSimpleUserBinding(binding: SimpleKeybinding | ScanCodeBinding | null): SimpleKeybinding | null { |
| 174 | + if (!binding) { |
| 175 | + return null; |
| 176 | + } |
| 177 | + if (binding instanceof SimpleKeybinding) { |
| 178 | + return binding; |
| 179 | + } |
| 180 | + const keyCode = this._scanCodeToKeyCode(binding.scanCode); |
| 181 | + if (keyCode === KeyCode.Unknown) { |
| 182 | + return null; |
| 183 | + } |
| 184 | + return new SimpleKeybinding(binding.ctrlKey, binding.shiftKey, binding.altKey, binding.metaKey, keyCode); |
| 185 | + } |
| 186 | + |
| 187 | + public static resolveUserBinding(input: (SimpleKeybinding | ScanCodeBinding)[], os: OperatingSystem): USLayoutResolvedKeybinding[] { |
| 188 | + const parts: SimpleKeybinding[] = removeElementsAfterNulls(input.map(keybinding => this._resolveSimpleUserBinding(keybinding))); |
| 189 | + if (parts.length > 0) { |
| 190 | + return [new USLayoutResolvedKeybinding(new ChordKeybinding(parts), os)]; |
| 191 | + } |
| 192 | + return []; |
| 193 | + } |
107 | 194 | }
|
0 commit comments