Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/keybinding/common/usLayoutResolvedKeybinding.ts
3296 views
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
6
import { KeyCode, KeyCodeUtils, IMMUTABLE_CODE_TO_KEY_CODE, ScanCode } from '../../../base/common/keyCodes.js';
7
import { SingleModifierChord, Chord, KeyCodeChord, Keybinding } from '../../../base/common/keybindings.js';
8
import { OperatingSystem } from '../../../base/common/platform.js';
9
import { BaseResolvedKeybinding } from './baseResolvedKeybinding.js';
10
import { toEmptyArrayIfContainsNull } from './resolvedKeybindingItem.js';
11
12
/**
13
* Do not instantiate. Use KeybindingService to get a ResolvedKeybinding seeded with information about the current kb layout.
14
*/
15
export class USLayoutResolvedKeybinding extends BaseResolvedKeybinding<KeyCodeChord> {
16
17
constructor(chords: KeyCodeChord[], os: OperatingSystem) {
18
super(os, chords);
19
}
20
21
private _keyCodeToUILabel(keyCode: KeyCode): string {
22
if (this._os === OperatingSystem.Macintosh) {
23
switch (keyCode) {
24
case KeyCode.LeftArrow:
25
return '←';
26
case KeyCode.UpArrow:
27
return '↑';
28
case KeyCode.RightArrow:
29
return '→';
30
case KeyCode.DownArrow:
31
return '↓';
32
}
33
}
34
return KeyCodeUtils.toString(keyCode);
35
}
36
37
protected _getLabel(chord: KeyCodeChord): string | null {
38
if (chord.isDuplicateModifierCase()) {
39
return '';
40
}
41
return this._keyCodeToUILabel(chord.keyCode);
42
}
43
44
protected _getAriaLabel(chord: KeyCodeChord): string | null {
45
if (chord.isDuplicateModifierCase()) {
46
return '';
47
}
48
return KeyCodeUtils.toString(chord.keyCode);
49
}
50
51
protected _getElectronAccelerator(chord: KeyCodeChord): string | null {
52
return KeyCodeUtils.toElectronAccelerator(chord.keyCode);
53
}
54
55
protected _getUserSettingsLabel(chord: KeyCodeChord): string | null {
56
if (chord.isDuplicateModifierCase()) {
57
return '';
58
}
59
const result = KeyCodeUtils.toUserSettingsUS(chord.keyCode);
60
return (result ? result.toLowerCase() : result);
61
}
62
63
protected _isWYSIWYG(): boolean {
64
return true;
65
}
66
67
protected _getChordDispatch(chord: KeyCodeChord): string | null {
68
return USLayoutResolvedKeybinding.getDispatchStr(chord);
69
}
70
71
public static getDispatchStr(chord: KeyCodeChord): string | null {
72
if (chord.isModifierKey()) {
73
return null;
74
}
75
let result = '';
76
77
if (chord.ctrlKey) {
78
result += 'ctrl+';
79
}
80
if (chord.shiftKey) {
81
result += 'shift+';
82
}
83
if (chord.altKey) {
84
result += 'alt+';
85
}
86
if (chord.metaKey) {
87
result += 'meta+';
88
}
89
result += KeyCodeUtils.toString(chord.keyCode);
90
91
return result;
92
}
93
94
protected _getSingleModifierChordDispatch(keybinding: KeyCodeChord): SingleModifierChord | null {
95
if (keybinding.keyCode === KeyCode.Ctrl && !keybinding.shiftKey && !keybinding.altKey && !keybinding.metaKey) {
96
return 'ctrl';
97
}
98
if (keybinding.keyCode === KeyCode.Shift && !keybinding.ctrlKey && !keybinding.altKey && !keybinding.metaKey) {
99
return 'shift';
100
}
101
if (keybinding.keyCode === KeyCode.Alt && !keybinding.ctrlKey && !keybinding.shiftKey && !keybinding.metaKey) {
102
return 'alt';
103
}
104
if (keybinding.keyCode === KeyCode.Meta && !keybinding.ctrlKey && !keybinding.shiftKey && !keybinding.altKey) {
105
return 'meta';
106
}
107
return null;
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.KeyA;
121
case ScanCode.KeyB: return KeyCode.KeyB;
122
case ScanCode.KeyC: return KeyCode.KeyC;
123
case ScanCode.KeyD: return KeyCode.KeyD;
124
case ScanCode.KeyE: return KeyCode.KeyE;
125
case ScanCode.KeyF: return KeyCode.KeyF;
126
case ScanCode.KeyG: return KeyCode.KeyG;
127
case ScanCode.KeyH: return KeyCode.KeyH;
128
case ScanCode.KeyI: return KeyCode.KeyI;
129
case ScanCode.KeyJ: return KeyCode.KeyJ;
130
case ScanCode.KeyK: return KeyCode.KeyK;
131
case ScanCode.KeyL: return KeyCode.KeyL;
132
case ScanCode.KeyM: return KeyCode.KeyM;
133
case ScanCode.KeyN: return KeyCode.KeyN;
134
case ScanCode.KeyO: return KeyCode.KeyO;
135
case ScanCode.KeyP: return KeyCode.KeyP;
136
case ScanCode.KeyQ: return KeyCode.KeyQ;
137
case ScanCode.KeyR: return KeyCode.KeyR;
138
case ScanCode.KeyS: return KeyCode.KeyS;
139
case ScanCode.KeyT: return KeyCode.KeyT;
140
case ScanCode.KeyU: return KeyCode.KeyU;
141
case ScanCode.KeyV: return KeyCode.KeyV;
142
case ScanCode.KeyW: return KeyCode.KeyW;
143
case ScanCode.KeyX: return KeyCode.KeyX;
144
case ScanCode.KeyY: return KeyCode.KeyY;
145
case ScanCode.KeyZ: return KeyCode.KeyZ;
146
case ScanCode.Digit1: return KeyCode.Digit1;
147
case ScanCode.Digit2: return KeyCode.Digit2;
148
case ScanCode.Digit3: return KeyCode.Digit3;
149
case ScanCode.Digit4: return KeyCode.Digit4;
150
case ScanCode.Digit5: return KeyCode.Digit5;
151
case ScanCode.Digit6: return KeyCode.Digit6;
152
case ScanCode.Digit7: return KeyCode.Digit7;
153
case ScanCode.Digit8: return KeyCode.Digit8;
154
case ScanCode.Digit9: return KeyCode.Digit9;
155
case ScanCode.Digit0: return KeyCode.Digit0;
156
case ScanCode.Minus: return KeyCode.Minus;
157
case ScanCode.Equal: return KeyCode.Equal;
158
case ScanCode.BracketLeft: return KeyCode.BracketLeft;
159
case ScanCode.BracketRight: return KeyCode.BracketRight;
160
case ScanCode.Backslash: return KeyCode.Backslash;
161
case ScanCode.IntlHash: return KeyCode.Unknown; // missing
162
case ScanCode.Semicolon: return KeyCode.Semicolon;
163
case ScanCode.Quote: return KeyCode.Quote;
164
case ScanCode.Backquote: return KeyCode.Backquote;
165
case ScanCode.Comma: return KeyCode.Comma;
166
case ScanCode.Period: return KeyCode.Period;
167
case ScanCode.Slash: return KeyCode.Slash;
168
case ScanCode.IntlBackslash: return KeyCode.IntlBackslash;
169
}
170
return KeyCode.Unknown;
171
}
172
173
private static _toKeyCodeChord(chord: Chord | null): KeyCodeChord | null {
174
if (!chord) {
175
return null;
176
}
177
if (chord instanceof KeyCodeChord) {
178
return chord;
179
}
180
const keyCode = this._scanCodeToKeyCode(chord.scanCode);
181
if (keyCode === KeyCode.Unknown) {
182
return null;
183
}
184
return new KeyCodeChord(chord.ctrlKey, chord.shiftKey, chord.altKey, chord.metaKey, keyCode);
185
}
186
187
public static resolveKeybinding(keybinding: Keybinding, os: OperatingSystem): USLayoutResolvedKeybinding[] {
188
const chords: KeyCodeChord[] = toEmptyArrayIfContainsNull(keybinding.chords.map(chord => this._toKeyCodeChord(chord)));
189
if (chords.length > 0) {
190
return [new USLayoutResolvedKeybinding(chords, os)];
191
}
192
return [];
193
}
194
}
195
196