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.
/*---------------------------------------------------------------------------------------------
keybindings.ts ×17
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { illegalArgument } from './errors.js';
import { KeyCode, ScanCode } from './keyCodes.js';
import { OperatingSystem } from './platform.js';
/**
* Binary encoding strategy:
* ```
* 1111 11
* 5432 1098 7654 3210
* ---- CSAW KKKK KKKK
* C = bit 11 = ctrlCmd flag
* S = bit 10 = shift flag
* A = bit 9 = alt flag
* W = bit 8 = winCtrl flag
* K = bits 0-7 = key code
* ```
*/
const enum BinaryKeybindingsMask {
CtrlCmd = (1 << 11) >>> 0,
Shift = (1 << 10) >>> 0,
Alt = (1 << 9) >>> 0,
WinCtrl = (1 << 8) >>> 0,
KeyCode = 0x000000FF
}
export function decodeKeybinding(keybinding: number | number[], OS: OperatingSystem): Keybinding | null {
if (keybinding === 0) {
}
const secondChord = (keybinding & 0xFFFF0000) >>> 16;
if (secondChord !== 0) {
createSimpleKeybinding(firstChord, OS),
createSimpleKeybinding(secondChord, OS)
]);
}
} else {
for (let i = 0; i < keybinding.length; i++) {
chords.push(createSimpleKeybinding(keybinding[i], OS));
}
return new Keybinding(chords);
}
export function createSimpleKeybinding(keybinding: number, OS: OperatingSystem): KeyCodeChord {
const ctrlCmd = (keybinding & BinaryKeybindingsMask.CtrlCmd ? true : false);
const winCtrl = (keybinding & BinaryKeybindingsMask.WinCtrl ? true : false);
const ctrlKey = (OS === OperatingSystem.Macintosh ? winCtrl : ctrlCmd);
const shiftKey = (keybinding & BinaryKeybindingsMask.Shift ? true : false);
const altKey = (keybinding & BinaryKeybindingsMask.Alt ? true : false);
const metaKey = (OS === OperatingSystem.Macintosh ? ctrlCmd : winCtrl);
const keyCode = (keybinding & BinaryKeybindingsMask.KeyCode);
return new KeyCodeChord(ctrlKey, shiftKey, altKey, metaKey, keyCode);
}
export interface Modifiers {
readonly ctrlKey: boolean;
readonly shiftKey: boolean;
readonly altKey: boolean;
readonly metaKey: boolean;
}
/**
* Represents a chord which uses the `keyCode` field of keyboard events.
* A chord is a combination of keys pressed simultaneously.
*/
export class KeyCodeChord implements Modifiers {
constructor(
public readonly shiftKey: boolean,
public readonly altKey: boolean,
public readonly metaKey: boolean,
public readonly keyCode: KeyCode
) { }
public equals(other: Chord): boolean {
return (
other instanceof KeyCodeChord
&& this.ctrlKey === other.ctrlKey
&& this.shiftKey === other.shiftKey
&& this.altKey === other.altKey
&& this.metaKey === other.metaKey
&& this.keyCode === other.keyCode
);
}
public getHashCode(): string {
const shift = this.shiftKey ? '1' : '0';
const alt = this.altKey ? '1' : '0';
const meta = this.metaKey ? '1' : '0';
return `K${ctrl}${shift}${alt}${meta}${this.keyCode}`;
}
public isModifierKey(): boolean {
this.keyCode === KeyCode.Unknown
|| this.keyCode === KeyCode.Ctrl
}
public toKeybinding(): Keybinding {
}
/**
* Does this keybinding refer to the key code of a modifier and it also has the modifier flag?
*/
public isDuplicateModifierCase(): boolean {
(this.ctrlKey && this.keyCode === KeyCode.Ctrl)
}
/**
* Represents a chord which uses the `code` field of keyboard events.
* A chord is a combination of keys pressed simultaneously.
*/
export class ScanCodeChord implements Modifiers {
constructor(
public readonly shiftKey: boolean,
public readonly altKey: boolean,
public readonly metaKey: boolean,
public readonly scanCode: ScanCode
) { }
public equals(other: Chord): boolean {
return (
other instanceof ScanCodeChord
&& this.ctrlKey === other.ctrlKey
&& this.shiftKey === other.shiftKey
&& this.altKey === other.altKey
&& this.metaKey === other.metaKey
&& this.scanCode === other.scanCode
);
}
public getHashCode(): string {
const shift = this.shiftKey ? '1' : '0';
const alt = this.altKey ? '1' : '0';
const meta = this.metaKey ? '1' : '0';
return `S${ctrl}${shift}${alt}${meta}${this.scanCode}`;
}
/**
* Does this keybinding refer to the key code of a modifier and it also has the modifier flag?
*/
public isDuplicateModifierCase(): boolean {
(this.ctrlKey && (this.scanCode === ScanCode.ControlLeft || this.scanCode === ScanCode.ControlRight))
|| (this.shiftKey && (this.scanCode === ScanCode.ShiftLeft || this.scanCode === ScanCode.ShiftRight))
keybindings.ts ×1
|| (this.altKey && (this.scanCode === ScanCode.AltLeft || this.scanCode === ScanCode.AltRight))
keybindings.ts ×1
|| (this.metaKey && (this.scanCode === ScanCode.MetaLeft || this.scanCode === ScanCode.MetaRight))
keybindings.ts ×1
}
export type Chord = KeyCodeChord | ScanCodeChord;
/**
* A keybinding is a sequence of chords.
*/
export class Keybinding {
public readonly chords: Chord[];
constructor(chords: Chord[]) {
throw illegalArgument(`chords`);
}
}
public getHashCode(): string {
let result = '';
for (let i = 0, len = this.chords.length; i < len; i++) {
if (i !== 0) {
result += ';';
}
result += this.chords[i].getHashCode();
}
return result;
}
public equals(other: Keybinding | null): boolean {
if (other === null) {
return false;
}
if (this.chords.length !== other.chords.length) {
return false;
}
for (let i = 0; i < this.chords.length; i++) {
if (!this.chords[i].equals(other.chords[i])) {
return false;
}
}
return true;
}
export class ResolvedChord {
constructor(
public readonly ctrlKey: boolean,
public readonly shiftKey: boolean,
public readonly altKey: boolean,
public readonly metaKey: boolean,
public readonly keyLabel: string | null,
public readonly keyAriaLabel: string | null
) { }
export type SingleModifierChord = 'ctrl' | 'shift' | 'alt' | 'meta';
/**
* A resolved keybinding. Consists of one or multiple chords.
*/
export abstract class ResolvedKeybinding {
/**
* This prints the binding in a format suitable for displaying in the UI.
*/
public abstract getLabel(): string | null;
/**
* This prints the binding in a format suitable for ARIA.
*/
public abstract getAriaLabel(): string | null;
/**
* This prints the binding in a format suitable for electron's accelerators.
* See https://github.com/electron/electron/blob/master/docs/api/accelerator.md
*/
public abstract getElectronAccelerator(): string | null;
/**
* This prints the binding in a format suitable for user settings.
*/
public abstract getUserSettingsLabel(): string | null;
/**
* Is the user settings label reflecting the label?
*/
public abstract isWYSIWYG(): boolean;
/**
* Does the keybinding consist of more than one chord?
*/
public abstract hasMultipleChords(): boolean;
/**
* Returns the chords that comprise of the keybinding.
*/
public abstract getChords(): ResolvedChord[];
/**
* Returns the chords as strings useful for dispatching.
* Returns null for modifier only chords.
* @example keybinding "Shift" -> null
* @example keybinding ("D" with shift == true) -> "shift+D"
*/
public abstract getDispatchChords(): (string | null)[];
/**
* Returns the modifier only chords as strings useful for dispatching.
* Returns null for chords that contain more than one modifier or a regular key.
* @example keybinding "Shift" -> "shift"
* @example keybinding ("D" with shift == true") -> null
*/
public abstract getSingleModifierDispatchChords(): (SingleModifierChord | null)[];
}