src/vs/base/common/keybindings.ts

284 LOC · 235 covered · 49 uncovered · 47 ranges · 1684 concepts · 24 introducers · 915 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- keybindings.ts ×17
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 { illegalArgument } from './errors.js';
7 > import { KeyCode, ScanCode } from './keyCodes.js';
8 > import { OperatingSystem } from './platform.js';
9 >
10 > /**
11 > * Binary encoding strategy:
12 > * ```
13 > * 1111 11
14 > * 5432 1098 7654 3210
15 > * ---- CSAW KKKK KKKK
16 > * C = bit 11 = ctrlCmd flag
17 > * S = bit 10 = shift flag
18 > * A = bit 9 = alt flag
19 > * W = bit 8 = winCtrl flag
20 > * K = bits 0-7 = key code
21 > * ```
22 > */
23 > const enum BinaryKeybindingsMask {
24 > CtrlCmd = (1 << 11) >>> 0,
25 > Shift = (1 << 10) >>> 0,
26 > Alt = (1 << 9) >>> 0,
27 > WinCtrl = (1 << 8) >>> 0,
28 > KeyCode = 0x000000FF
29 > }
30 >
31 > export function decodeKeybinding(keybinding: number | number[], OS: OperatingSystem): Keybinding | null {
32 > if (typeof keybinding === 'number') { keybindings.ts ×3
33 > if (keybinding === 0) {
34 > return null; keybindings.ts ×1
35 > }
36 > const firstChord = (keybinding & 0x0000FFFF) >>> 0; keybindings.ts ×3
37 > const secondChord = (keybinding & 0xFFFF0000) >>> 16;
38 > if (secondChord !== 0) {
39 > return new Keybinding([ keyCodes.ts ×1
40 > createSimpleKeybinding(firstChord, OS),
41 > createSimpleKeybinding(secondChord, OS)
42 > ]);
43 > }
44 > return new Keybinding([createSimpleKeybinding(firstChord, OS)]); keybindings.ts ×1
45 > } else {
46 > const chords = []; keybindings.ts ×1
47 > for (let i = 0; i < keybinding.length; i++) {
48 > chords.push(createSimpleKeybinding(keybinding[i], OS));
49 > }
50 > return new Keybinding(chords);
51 > }
54 > export function createSimpleKeybinding(keybinding: number, OS: OperatingSystem): KeyCodeChord {
56 > const ctrlCmd = (keybinding & BinaryKeybindingsMask.CtrlCmd ? true : false);
57 > const winCtrl = (keybinding & BinaryKeybindingsMask.WinCtrl ? true : false);
58 >
59 > const ctrlKey = (OS === OperatingSystem.Macintosh ? winCtrl : ctrlCmd);
60 > const shiftKey = (keybinding & BinaryKeybindingsMask.Shift ? true : false);
61 > const altKey = (keybinding & BinaryKeybindingsMask.Alt ? true : false);
62 > const metaKey = (OS === OperatingSystem.Macintosh ? ctrlCmd : winCtrl);
63 > const keyCode = (keybinding & BinaryKeybindingsMask.KeyCode);
64 >
65 > return new KeyCodeChord(ctrlKey, shiftKey, altKey, metaKey, keyCode);
66 > }
68 > export interface Modifiers {
69 > readonly ctrlKey: boolean;
70 > readonly shiftKey: boolean;
71 > readonly altKey: boolean;
72 > readonly metaKey: boolean;
73 > }
74 >
75 > /**
76 > * Represents a chord which uses the `keyCode` field of keyboard events.
77 > * A chord is a combination of keys pressed simultaneously.
78 > */
79 > export class KeyCodeChord implements Modifiers {
80 >
81 > constructor(
82 > public readonly ctrlKey: boolean, keybindings.ts ×1
83 > public readonly shiftKey: boolean,
84 > public readonly altKey: boolean,
85 > public readonly metaKey: boolean,
86 > public readonly keyCode: KeyCode
87 > ) { }
89 > public equals(other: Chord): boolean {
90 return (
91 other instanceof KeyCodeChord
92 && this.ctrlKey === other.ctrlKey
93 && this.shiftKey === other.shiftKey
94 && this.altKey === other.altKey
95 && this.metaKey === other.metaKey
96 && this.keyCode === other.keyCode
97 );
98 }
100 > public getHashCode(): string {
101 > const ctrl = this.ctrlKey ? '1' : '0'; keybindings.ts ×2
102 > const shift = this.shiftKey ? '1' : '0';
103 > const alt = this.altKey ? '1' : '0';
104 > const meta = this.metaKey ? '1' : '0';
105 > return `K${ctrl}${shift}${alt}${meta}${this.keyCode}`;
106 > }
108 > public isModifierKey(): boolean {
109 > return ( keybindings.ts ×2
110 > this.keyCode === KeyCode.Unknown
111 > || this.keyCode === KeyCode.Ctrl
112 > || this.keyCode === KeyCode.Meta keybindings.ts ×1
113 > || this.keyCode === KeyCode.Alt keybindings.ts ×1
114 > || this.keyCode === KeyCode.Shift keybindings.ts ×1
116 > }
118 > public toKeybinding(): Keybinding {
119 > return new Keybinding([this]); keybindings.ts ×1
120 > }
122 > /**
123 > * Does this keybinding refer to the key code of a modifier and it also has the modifier flag?
124 > */
125 > public isDuplicateModifierCase(): boolean {
126 > return ( keybindings.ts ×2
127 > (this.ctrlKey && this.keyCode === KeyCode.Ctrl)
128 > || (this.shiftKey && this.keyCode === KeyCode.Shift) keybindings.ts ×1
129 > || (this.altKey && this.keyCode === KeyCode.Alt) keybindings.ts ×1
130 > || (this.metaKey && this.keyCode === KeyCode.Meta) keybindings.ts ×1
132 > }
134 >
135 > /**
136 > * Represents a chord which uses the `code` field of keyboard events.
137 > * A chord is a combination of keys pressed simultaneously.
138 > */
139 > export class ScanCodeChord implements Modifiers {
140 >
141 > constructor(
142 > public readonly ctrlKey: boolean, keybindings.ts ×1
143 > public readonly shiftKey: boolean,
144 > public readonly altKey: boolean,
145 > public readonly metaKey: boolean,
146 > public readonly scanCode: ScanCode
147 > ) { }
149 > public equals(other: Chord): boolean {
150 return (
151 other instanceof ScanCodeChord
152 && this.ctrlKey === other.ctrlKey
153 && this.shiftKey === other.shiftKey
154 && this.altKey === other.altKey
155 && this.metaKey === other.metaKey
156 && this.scanCode === other.scanCode
157 );
158 }
160 > public getHashCode(): string {
161 > const ctrl = this.ctrlKey ? '1' : '0'; keybindings.ts ×2
162 > const shift = this.shiftKey ? '1' : '0';
163 > const alt = this.altKey ? '1' : '0';
164 > const meta = this.metaKey ? '1' : '0';
165 > return `S${ctrl}${shift}${alt}${meta}${this.scanCode}`;
166 > }
168 > /**
169 > * Does this keybinding refer to the key code of a modifier and it also has the modifier flag?
170 > */
171 > public isDuplicateModifierCase(): boolean {
173 > (this.ctrlKey && (this.scanCode === ScanCode.ControlLeft || this.scanCode === ScanCode.ControlRight))
174 > || (this.shiftKey && (this.scanCode === ScanCode.ShiftLeft || this.scanCode === ScanCode.ShiftRight)) keybindings.ts ×1
175 > || (this.altKey && (this.scanCode === ScanCode.AltLeft || this.scanCode === ScanCode.AltRight)) keybindings.ts ×1
176 > || (this.metaKey && (this.scanCode === ScanCode.MetaLeft || this.scanCode === ScanCode.MetaRight)) keybindings.ts ×1
178 > }
180 >
181 > export type Chord = KeyCodeChord | ScanCodeChord;
182 >
183 > /**
184 > * A keybinding is a sequence of chords.
185 > */
186 > export class Keybinding {
187 >
188 > public readonly chords: Chord[];
189 >
190 > constructor(chords: Chord[]) {
191 > if (chords.length === 0) { keybindings.ts ×2
192 throw illegalArgument(`chords`);
193 }
194 > this.chords = chords; keybindings.ts ×2
195 > }
197 > public getHashCode(): string {
198 let result = '';
199 for (let i = 0, len = this.chords.length; i < len; i++) {
200 if (i !== 0) {
201 result += ';';
202 }
203 result += this.chords[i].getHashCode();
204 }
205 return result;
206 }
208 > public equals(other: Keybinding | null): boolean {
209 if (other === null) {
210 return false;
211 }
212 if (this.chords.length !== other.chords.length) {
213 return false;
214 }
215 for (let i = 0; i < this.chords.length; i++) {
216 if (!this.chords[i].equals(other.chords[i])) {
217 return false;
218 }
219 }
220 return true;
221 }
223 >
224 > export class ResolvedChord {
225 > constructor(
226 public readonly ctrlKey: boolean,
227 public readonly shiftKey: boolean,
228 public readonly altKey: boolean,
229 public readonly metaKey: boolean,
230 public readonly keyLabel: string | null,
231 public readonly keyAriaLabel: string | null
232 ) { }
234 >
235 > export type SingleModifierChord = 'ctrl' | 'shift' | 'alt' | 'meta';
236 >
237 > /**
238 > * A resolved keybinding. Consists of one or multiple chords.
239 > */
240 > export abstract class ResolvedKeybinding {
241 > /**
242 > * This prints the binding in a format suitable for displaying in the UI.
243 > */
244 > public abstract getLabel(): string | null;
245 > /**
246 > * This prints the binding in a format suitable for ARIA.
247 > */
248 > public abstract getAriaLabel(): string | null;
249 > /**
250 > * This prints the binding in a format suitable for electron's accelerators.
251 > * See https://github.com/electron/electron/blob/master/docs/api/accelerator.md
252 > */
253 > public abstract getElectronAccelerator(): string | null;
254 > /**
255 > * This prints the binding in a format suitable for user settings.
256 > */
257 > public abstract getUserSettingsLabel(): string | null;
258 > /**
259 > * Is the user settings label reflecting the label?
260 > */
261 > public abstract isWYSIWYG(): boolean;
262 > /**
263 > * Does the keybinding consist of more than one chord?
264 > */
265 > public abstract hasMultipleChords(): boolean;
266 > /**
267 > * Returns the chords that comprise of the keybinding.
268 > */
269 > public abstract getChords(): ResolvedChord[];
270 > /**
271 > * Returns the chords as strings useful for dispatching.
272 > * Returns null for modifier only chords.
273 > * @example keybinding "Shift" -> null
274 > * @example keybinding ("D" with shift == true) -> "shift+D"
275 > */
276 > public abstract getDispatchChords(): (string | null)[];
277 > /**
278 > * Returns the modifier only chords as strings useful for dispatching.
279 > * Returns null for chords that contain more than one modifier or a regular key.
280 > * @example keybinding "Shift" -> "shift"
281 > * @example keybinding ("D" with shift == true") -> null
282 > */
283 > public abstract getSingleModifierDispatchChords(): (SingleModifierChord | null)[];
284 > }