@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
863 lines • 36.5 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2017-2019 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { nls } from './nls.js';
import { isOSX } from './os.js';
import { isObject } from './types.js';
export var KeySequence;
(function (KeySequence) {
function equals(a, b) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (!a[i].equals(b[i])) {
return false;
}
}
return true;
}
KeySequence.equals = equals;
let CompareResult;
(function (CompareResult) {
CompareResult[CompareResult["NONE"] = 0] = "NONE";
CompareResult[CompareResult["PARTIAL"] = 1] = "PARTIAL";
CompareResult[CompareResult["SHADOW"] = 2] = "SHADOW";
CompareResult[CompareResult["FULL"] = 3] = "FULL";
})(CompareResult = KeySequence.CompareResult || (KeySequence.CompareResult = {}));
/* Compares two KeySequences, returns:
* FULL if the KeySequences are the same.
* PARTIAL if the KeySequence a part of b.
* SHADOW if the KeySequence b part of a.
* NONE if the KeySequences are not the same at all.
*/
function compare(a, b) {
let first = a;
let second = b;
let shadow = false;
if (b.length < a.length) {
first = b;
second = a;
shadow = true;
}
for (let i = 0; i < first.length; i++) {
if (first[i].equals(second[i]) === false) {
return KeySequence.CompareResult.NONE;
}
}
if (first.length < second.length) {
if (shadow === false) {
return KeySequence.CompareResult.PARTIAL;
}
else {
return KeySequence.CompareResult.SHADOW;
}
}
return KeySequence.CompareResult.FULL;
}
KeySequence.compare = compare;
function parse(keybinding) {
const keyCodes = [];
const rawKeyCodes = splitKeySequence(keybinding.trim());
for (const rawKeyCode of rawKeyCodes) {
const keyCode = KeyCode.parse(rawKeyCode);
if (keyCode !== undefined) {
keyCodes.push(keyCode);
}
}
return keyCodes;
}
KeySequence.parse = parse;
})(KeySequence || (KeySequence = {}));
export function layoutModifiersIncludeShift(layoutModifiers) {
return layoutModifiers === 'shift' || layoutModifiers === 'shiftAltGraph';
}
export function layoutModifiersIncludeAltGraph(layoutModifiers) {
return layoutModifiers === 'altGraph' || layoutModifiers === 'shiftAltGraph';
}
export function isPrintableKey(key) {
return isPrintableCharacter(key.easyString);
}
export function isPrintableCharacter(character) {
return !!character && character !== ' ' && isSingleCharacter(character);
}
/**
* Read AltGraph state, preferring explicitly transported state over native event state.
*/
export function readAltGraph(input) {
if (input.altGraph !== undefined) {
return input.altGraph;
}
return input.getModifierState?.('AltGraph') ?? false;
}
/**
* Copy native or transported keyboard input into a normalized, transport-independent representation.
* The parameter and result share this type because a native `KeyboardEvent` structurally satisfies the permissive input
* shape. The returned data always has AltGraph resolved even though the field is optional in the input type.
* `getModifierState` is retained for local diagnostics only and must not be relied on after transport.
*/
export function normalizeKeyboardInput(input) {
return {
key: input.key,
code: input.code,
keyCode: input.keyCode,
charCode: input.charCode,
ctrlKey: input.ctrlKey,
shiftKey: input.shiftKey,
altKey: input.altKey,
metaKey: input.metaKey,
altGraph: readAltGraph(input),
repeat: input.repeat,
isComposing: input.isComposing,
location: input.location,
timeStamp: input.timeStamp,
keyIdentifier: input.keyIdentifier,
getModifierState: input.getModifierState?.bind(input)
};
}
function splitKeySequence(keybinding) {
const result = [];
let current = '';
let bracketed = false;
for (let index = 0; index < keybinding.length; index++) {
const character = keybinding[index];
if (character === '[') {
const closingBracket = keybinding.indexOf(']', index + 1);
const nextWhitespace = keybinding.slice(index + 1).search(/\s/);
bracketed = closingBracket !== -1 && (nextWhitespace === -1 || closingBracket < index + 1 + nextWhitespace);
}
else if (character === ']' && bracketed) {
bracketed = false;
}
if (/\s/.test(character) && !bracketed) {
if (current) {
result.push(current);
current = '';
}
}
else {
current += character;
}
}
if (current) {
result.push(current);
}
return result;
}
function splitKeyCode(keybinding) {
const result = [];
let current = '';
let bracketed = false;
for (let index = 0; index < keybinding.length; index++) {
const character = keybinding[index];
if (character === '[' && keybinding.indexOf(']', index + 1) !== -1) {
bracketed = true;
}
else if (character === ']' && bracketed) {
bracketed = false;
}
if (!bracketed && current && (character === '+' || character === '-')) {
result.push(current);
current = '';
}
else if (!bracketed && character === '+') {
continue;
}
else {
current += character;
}
}
if (current) {
result.push(current);
}
return result;
}
function parseScanCode(token) {
const match = /^\[([^\]]+)\]$/.exec(token);
if (!match || match[1].toLocaleLowerCase().startsWith('char:')) {
return undefined;
}
const key = Key.getKey(match[1]) ?? Object.values(CODE_TO_KEY).find(candidate => candidate.code.toLocaleLowerCase() === match[1].toLocaleLowerCase());
if (!key) {
throw new Error(nls.localize('theia/core/keybinding/unknownScanCode', 'Unknown scan code {0}. Use a supported KeyboardEvent.code name.', match[1]));
}
return key;
}
/** Parse a logical-character token, rejecting bare grammar delimiters. */
function parseCharacterToken(token) {
const match = /^\[char:(.*)\]$/i.exec(token);
if (match) {
const payload = match[1];
let character;
if (/^0x[0-9a-f]+$/i.test(payload)) {
const value = Number.parseInt(payload.slice(2), 16);
if (value > 0x10ffff || value >= 0xd800 && value <= 0xdfff) {
throw new Error(nls.localize('theia/core/keybinding/invalidCharacter', 'Invalid Unicode character {0}.', payload));
}
character = String.fromCodePoint(value);
}
else {
character = payload;
}
if (!isSingleCharacter(character) || !/^0x/i.test(payload) && RESERVED_BARE_CHARACTERS.has(character)) {
throw new Error(nls.localize('theia/core/keybinding/invalidCharacter', 'Invalid Unicode character {0}.', payload));
}
return character;
}
return isSingleCharacter(token) && !RESERVED_BARE_CHARACTERS.has(token) ? token : undefined;
}
function isSingleCharacter(value) {
return Array.from(value).length === 1;
}
/** Characters reserved by the keybinding grammar and therefore unavailable as bare logical-character tokens. */
const RESERVED_BARE_CHARACTERS = new Set([' ', '+', '-', '[', ']']);
/**
* Serialize a logical character for the keybinding grammar.
* `+` and `-` are excluded from legacy bare spelling because they delimit key-code tokens. Uppercase ASCII letters are
* escaped so their case is not lost by case-insensitive legacy key lookup.
*/
export function characterToken(character) {
if (!isSingleCharacter(character)) {
throw new Error(nls.localize('theia/core/keybinding/invalidCharacter', 'Invalid Unicode character {0}.', character));
}
const legacyCharacter = !!EASY_TO_KEY[character] && character !== '+' && character !== '-';
const requiresEscape = /^[A-Z]$/.test(character) || RESERVED_BARE_CHARACTERS.has(character) && !legacyCharacter;
return requiresEscape
? `[char:0x${character.codePointAt(0).toString(16).toUpperCase()}]`
: character;
}
/**
* Representation of a pressed key combined with key modifiers.
*/
export class KeyCode {
constructor(schema) {
const key = schema.key;
if (key) {
if (key.code && key.keyCode && key.easyString) {
this.key = key;
}
else if (key.code) {
this.key = Key.getKey(key.code);
}
else if (key.keyCode) {
this.key = Key.getKey(key.keyCode);
}
}
this.ctrl = !!schema.ctrl;
this.shift = !!schema.shift;
this.alt = !!schema.alt;
this.meta = !!schema.meta;
this.character = schema.character;
this.layoutModifiers = schema.layoutModifiers ?? 'none';
this.interpretation = schema.interpretation ?? 'authored';
this.supportedByLayout = schema.supportedByLayout !== false;
this.authoredToken = schema.authoredToken;
this.physical = !!schema.physical;
}
/**
* Return true if this KeyCode only contains modifiers.
*/
isModifierOnly() {
return this.key === undefined && this.character === undefined;
}
/**
* Return true if the given KeyCode is equal to this one.
*/
equals(other) {
if (this.key && (!other.key || this.key.code !== other.key.code) || !this.key && other.key) {
return false;
}
return this.dispatchString() === other.dispatchString();
}
/**
* Return a keybinding string that round-trips the authored spelling through {@link authoredToken}.
*/
toString() {
const result = [];
if (this.meta) {
result.push(SpecialCases.META);
}
if (this.shift) {
result.push(Key.SHIFT_LEFT.easyString);
}
if (this.alt) {
result.push(Key.ALT_LEFT.easyString);
}
if (this.ctrl) {
result.push(Key.CONTROL_LEFT.easyString);
}
if (this.authoredToken) {
result.push(this.authoredToken);
}
else if (this.key) {
result.push(this.key.easyString);
}
return result.join('+');
}
/**
* Return the runtime match identity used for keybinding dispatch.
* The interpretation is diagnostic metadata and does not affect this identity.
*/
dispatchString() {
const result = [];
if (this.meta) {
result.push(SpecialCases.META);
}
if (this.shift) {
result.push(Key.SHIFT_LEFT.easyString);
}
if (this.alt) {
result.push(Key.ALT_LEFT.easyString);
}
if (this.ctrl) {
result.push(Key.CONTROL_LEFT.easyString);
}
if (this.key) {
result.push(this.key.easyString);
}
else if (this.character) {
result.push(characterToken(this.character));
}
const suffix = this.layoutModifiers === 'shift' ? '@shift'
: this.layoutModifiers === 'altGraph' ? '@altgraph'
: this.layoutModifiers === 'shiftAltGraph' ? '@shift@altgraph' : '';
return result.join('+') + suffix;
}
/**
* Derive an authored keybinding stroke from a key event for recorder persistence.
* This cannot use {@link toString}: event-derived key codes have no {@link authoredToken}, so `toString` would persist
* the physical US key name instead of the committed logical character. Non-printable keys, Space and numpad keys are
* persisted by name; printable keys without a committed character are persisted as scan codes.
*/
toAuthoredKeybindingString() {
const result = [];
if (this.meta) {
result.push(SpecialCases.META);
}
const character = isPrintableCharacter(this.character) && !this.key?.code.startsWith('Numpad') ? this.character : undefined;
const shiftedLetter = !!character && /^[A-Z]$/.test(character);
if (this.shift && (!character || shiftedLetter)) {
result.push(Key.SHIFT_LEFT.easyString);
}
if (this.alt) {
result.push(Key.ALT_LEFT.easyString);
}
if (this.ctrl) {
result.push(Key.CONTROL_LEFT.easyString);
}
if (character) {
result.push(characterToken(shiftedLetter ? character.toLocaleLowerCase() : character));
}
else if (this.key) {
result.push(isPrintableKey(this.key) ? `[${this.key.code}]` : this.key.easyString);
}
return result.join('+');
}
/**
* Create a KeyCode from one of several input types.
*/
static createKeyCode(input, eventDispatch = 'code') {
if (typeof input === 'string') {
const parts = input.split('+');
if (!KeyCode.isModifierString(parts[0])) {
return KeyCode.createKeyCode({
first: Key.getKey(parts[0]),
modifiers: parts.slice(1)
});
}
return KeyCode.createKeyCode({ modifiers: parts });
}
else if (KeyCode.isKeyboardEvent(input) || KeyCode.isNormalizedKeyboardInput(input)) {
const key = KeyCode.toKey(input, eventDispatch);
return new KeyCode({
key: Key.isModifier(key.code) ? undefined : key,
meta: isOSX && input.metaKey,
shift: input.shiftKey,
alt: input.altKey,
ctrl: input.ctrlKey,
character: KeyCode.toCharacter(input)
});
}
else if (input.first || input.modifiers) {
const keystroke = input;
const schema = {
key: keystroke.first
};
if (keystroke.modifiers) {
if (isOSX) {
schema.meta = keystroke.modifiers.some(mod => mod === KeyModifier.CtrlCmd);
schema.ctrl = keystroke.modifiers.some(mod => mod === KeyModifier.MacCtrl);
}
else {
schema.meta = false;
schema.ctrl = keystroke.modifiers.some(mod => mod === KeyModifier.CtrlCmd);
}
schema.shift = keystroke.modifiers.some(mod => mod === KeyModifier.Shift);
schema.alt = keystroke.modifiers.some(mod => mod === KeyModifier.Alt);
}
return new KeyCode(schema);
}
else {
return new KeyCode(input);
}
}
static { this.keybindings = {}; }
/* Reset the key hashmap, this is for testing purposes. */
static resetKeyBindings() {
KeyCode.keybindings = {};
}
/**
* Parses a string and returns a KeyCode object.
* @param keybinding String representation of a keybinding
*/
static parse(keybinding) {
if (KeyCode.keybindings[keybinding]) {
return KeyCode.keybindings[keybinding];
}
const schema = {};
const keys = splitKeyCode(keybinding.trim());
/* If duplicates i.e ctrl+ctrl+a or alt+alt+b or b+alt+b it is invalid */
if (keys.length !== new Set(keys.map(key => key.toLocaleLowerCase())).size) {
throw new Error(nls.localize('theia/core/keybinding/duplicateModifierError', "Can't parse keybinding {0} Duplicate modifiers", keybinding));
}
for (const token of keys) {
let keyString = token.toLocaleLowerCase();
if (SPECIAL_ALIASES[keyString] !== undefined) {
keyString = SPECIAL_ALIASES[keyString];
}
const key = EASY_TO_KEY[keyString];
/* meta only works on macOS */
if (keyString === SpecialCases.META) {
if (isOSX) {
schema.meta = true;
}
else {
throw new Error(nls.localize('theia/core/keybinding/metaError', "Can't parse keybinding {0} meta is for OSX only", keybinding));
}
/* ctrlcmd for M1 keybindings that work on both macOS and other platforms */
}
else if (keyString === SpecialCases.CTRLCMD) {
if (isOSX) {
schema.meta = true;
}
else {
schema.ctrl = true;
}
}
else if (Key.isKey(key)) {
if (Key.isModifier(key.code)) {
if (key.code === Key.CONTROL_LEFT.code || key.code === Key.CONTROL_RIGHT.code) {
schema.ctrl = true;
}
else if (key.code === Key.SHIFT_LEFT.code || key.code === Key.SHIFT_RIGHT.code) {
schema.shift = true;
}
else if (key.code === Key.ALT_LEFT.code || key.code === Key.ALT_RIGHT.code) {
schema.alt = true;
}
}
else {
schema.key = key;
}
}
else {
const scanCode = parseScanCode(token);
if (scanCode) {
schema.key = scanCode;
schema.authoredToken = `[${scanCode.code}]`;
schema.physical = true;
continue;
}
const character = parseCharacterToken(token);
if (character) {
schema.character = character;
schema.authoredToken = characterToken(character);
continue;
}
throw new Error(nls.localize('theia/core/keybinding/unrecognizedKeyError', 'Unrecognized key {0} in {1}', token, keybinding));
}
}
KeyCode.keybindings[keybinding] = new KeyCode(schema);
return KeyCode.keybindings[keybinding];
}
}
(function (KeyCode) {
/*
* Return true if the string is a modifier M1 to M4.
*/
function isModifierString(key) {
return key === KeyModifier.CtrlCmd
|| key === KeyModifier.Shift
|| key === KeyModifier.Alt
|| key === KeyModifier.MacCtrl;
}
KeyCode.isModifierString = isModifierString;
/**
* Different scopes have different execution environments. This means that they have different built-ins
* (different global object, different constructors, etc.). This may result in unexpected results. For instance,
* `[] instanceof window.frames[0].Array` will return `false`, because `Array.prototype !== window.frames[0].Array`
* and arrays inherit from the former.
* See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof
*
* Note: just add another check if the current `event.type` checking is insufficient.
*/
function isKeyboardEvent(event) {
if (typeof KeyboardEvent === 'undefined') { // This can happen in tests
return false;
}
if (event instanceof KeyboardEvent) {
return true;
}
const { type } = event;
if (type) {
return type === 'keypress' || type === 'keydown' || type === 'keyup';
}
return false;
}
KeyCode.isKeyboardEvent = isKeyboardEvent;
/**
* Return true if the given object carries normalized keyboard input fields.
*/
function isNormalizedKeyboardInput(input) {
return 'code' in input || 'keyCode' in input || 'charCode' in input || 'keyIdentifier' in input;
}
KeyCode.isNormalizedKeyboardInput = isNormalizedKeyboardInput;
/**
* Determine the pressed key of a keyboard event. This key should correspond to the physical key according
* to a standard US keyboard layout. International keyboard layouts are handled by `KeyboardLayoutService`.
*
* `keyIdentifier` is used to access this deprecated field:
* https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyIdentifier
*/
function toKey(event, dispatch = 'code') {
const code = event.code;
if (code && dispatch === 'code') {
if (isOSX) {
// https://github.com/eclipse-theia/theia/issues/4986
const char = event.key;
if (code === 'IntlBackslash' && (char === '`' || char === '~')) {
return Key.BACKQUOTE;
}
else if (code === 'Backquote' && (char === '§' || char === '±')) {
return Key.INTL_BACKSLASH;
}
}
// https://github.com/eclipse-theia/theia/issues/7315
if (code.startsWith('Numpad') && event.key && event.key.length > 1) {
const k = Key.getKey(event.key);
if (k) {
return k;
}
}
const key = Key.getKey(code);
if (key) {
return key;
}
}
// eslint-disable-next-line @typescript-eslint/no-deprecated
const keyCode = event.keyCode;
if (keyCode) {
const key = Key.getKey(keyCode);
if (key) {
return key;
}
}
const keyIdentifier = event.keyIdentifier;
if (keyIdentifier) {
const key = Key.getKey(keyIdentifier);
if (key) {
return key;
}
}
throw new Error(`Cannot get key code from the keyboard event: ${event}.`);
}
KeyCode.toKey = toKey;
/**
* Determine the actual printable character that is generated from a pressed key.
* If the key does not correspond to a printable character, `undefined` is returned.
* The result may be altered by modifier keys.
*/
function toCharacter(event) {
const key = event.key;
// Use the key property if it contains exactly one unicode character
if (key && Array.from(key).length === 1) {
return key;
}
const charCode = event.charCode;
// Use the charCode property if it does not correspond to a unicode control character
if (charCode && charCode > 0x1f && !(charCode >= 0x80 && charCode <= 0x9f)) {
return String.fromCharCode(charCode);
}
return undefined;
}
KeyCode.toCharacter = toCharacter;
})(KeyCode || (KeyCode = {}));
export var KeyModifier;
(function (KeyModifier) {
/**
* M1 is the COMMAND key on MacOS X, and the CTRL key on most other platforms.
*/
KeyModifier["CtrlCmd"] = "M1";
/**
* M2 is the SHIFT key.
*/
KeyModifier["Shift"] = "M2";
/**
* M3 is the Option key on MacOS X, and the ALT key on most other platforms.
*/
KeyModifier["Alt"] = "M3";
/**
* M4 is the CTRL key on MacOS X, and is undefined on other platforms.
*/
KeyModifier["MacCtrl"] = "M4";
})(KeyModifier || (KeyModifier = {}));
(function (KeyModifier) {
/**
* The CTRL key, independently of the platform.
* _Note:_ In general `KeyModifier.CtrlCmd` should be preferred over this constant.
*/
KeyModifier.CTRL = isOSX ? KeyModifier.MacCtrl : KeyModifier.CtrlCmd;
/**
* An alias for the SHIFT key (`KeyModifier.Shift`).
*/
KeyModifier.SHIFT = KeyModifier.Shift;
/**
* `true` if the argument represents a modifier. Otherwise, `false`.
*/
function isModifier(key) {
if (key) {
switch (key) {
case 'M1': // Fall through.
case 'M2': // Fall through.
case 'M3': // Fall through.
case 'M4': return true;
default: return false;
}
}
return false;
}
KeyModifier.isModifier = isModifier;
})(KeyModifier || (KeyModifier = {}));
const CODE_TO_KEY = {};
const KEY_CODE_TO_KEY = {};
const EASY_TO_KEY = {}; // From 'ctrl' to Key structure
const MODIFIERS = [];
const SPECIAL_ALIASES = {
'option': 'alt',
'command': 'meta',
'cmd': 'meta',
'return': 'enter',
'esc': 'escape',
'mod': 'ctrl',
'ins': 'insert',
'del': 'delete',
'control': 'ctrl',
};
export var SpecialCases;
(function (SpecialCases) {
SpecialCases.META = 'meta';
SpecialCases.CTRLCMD = 'ctrlcmd';
})(SpecialCases || (SpecialCases = {}));
export var Key;
(function (Key) {
function isKey(arg) {
return isObject(arg) && 'code' in arg && 'keyCode' in arg;
}
Key.isKey = isKey;
function getKey(arg) {
if (typeof arg === 'number') {
return KEY_CODE_TO_KEY[arg];
}
else {
return CODE_TO_KEY[arg];
}
}
Key.getKey = getKey;
function isModifier(arg) {
if (typeof arg === 'number') {
return MODIFIERS.find(key => key.keyCode === arg) !== undefined;
}
return MODIFIERS.find(key => key.code === arg) !== undefined;
}
Key.isModifier = isModifier;
function equals(key, keyCode) {
return !!keyCode.key && key.keyCode === keyCode.key.keyCode;
}
Key.equals = equals;
Key.BACKSPACE = { code: 'Backspace', keyCode: 8, easyString: 'backspace' };
Key.TAB = { code: 'Tab', keyCode: 9, easyString: 'tab' };
Key.ENTER = { code: 'Enter', keyCode: 13, easyString: 'enter' };
Key.ESCAPE = { code: 'Escape', keyCode: 27, easyString: 'escape' };
Key.SPACE = { code: 'Space', keyCode: 32, easyString: 'space' };
Key.PAGE_UP = { code: 'PageUp', keyCode: 33, easyString: 'pageup' };
Key.PAGE_DOWN = { code: 'PageDown', keyCode: 34, easyString: 'pagedown' };
Key.END = { code: 'End', keyCode: 35, easyString: 'end' };
Key.HOME = { code: 'Home', keyCode: 36, easyString: 'home' };
Key.ARROW_LEFT = { code: 'ArrowLeft', keyCode: 37, easyString: 'left' };
Key.ARROW_UP = { code: 'ArrowUp', keyCode: 38, easyString: 'up' };
Key.ARROW_RIGHT = { code: 'ArrowRight', keyCode: 39, easyString: 'right' };
Key.ARROW_DOWN = { code: 'ArrowDown', keyCode: 40, easyString: 'down' };
Key.INSERT = { code: 'Insert', keyCode: 45, easyString: 'insert' };
Key.DELETE = { code: 'Delete', keyCode: 46, easyString: 'delete' };
Key.SHIFT_LEFT = { code: 'ShiftLeft', keyCode: 16, easyString: 'shift' };
Key.SHIFT_RIGHT = { code: 'ShiftRight', keyCode: 16, easyString: 'shift' };
Key.CONTROL_LEFT = { code: 'ControlLeft', keyCode: 17, easyString: 'ctrl' };
Key.CONTROL_RIGHT = { code: 'ControlRight', keyCode: 17, easyString: 'ctrl' };
Key.ALT_LEFT = { code: 'AltLeft', keyCode: 18, easyString: 'alt' };
Key.ALT_RIGHT = { code: 'AltRight', keyCode: 18, easyString: 'alt' };
Key.CAPS_LOCK = { code: 'CapsLock', keyCode: 20, easyString: 'capslock' };
Key.OS_LEFT = { code: 'OSLeft', keyCode: 91, easyString: 'super' };
Key.OS_RIGHT = { code: 'OSRight', keyCode: 92, easyString: 'super' };
Key.DIGIT0 = { code: 'Digit0', keyCode: 48, easyString: '0' };
Key.DIGIT1 = { code: 'Digit1', keyCode: 49, easyString: '1' };
Key.DIGIT2 = { code: 'Digit2', keyCode: 50, easyString: '2' };
Key.DIGIT3 = { code: 'Digit3', keyCode: 51, easyString: '3' };
Key.DIGIT4 = { code: 'Digit4', keyCode: 52, easyString: '4' };
Key.DIGIT5 = { code: 'Digit5', keyCode: 53, easyString: '5' };
Key.DIGIT6 = { code: 'Digit6', keyCode: 54, easyString: '6' };
Key.DIGIT7 = { code: 'Digit7', keyCode: 55, easyString: '7' };
Key.DIGIT8 = { code: 'Digit8', keyCode: 56, easyString: '8' };
Key.DIGIT9 = { code: 'Digit9', keyCode: 57, easyString: '9' };
Key.KEY_A = { code: 'KeyA', keyCode: 65, easyString: 'a' };
Key.KEY_B = { code: 'KeyB', keyCode: 66, easyString: 'b' };
Key.KEY_C = { code: 'KeyC', keyCode: 67, easyString: 'c' };
Key.KEY_D = { code: 'KeyD', keyCode: 68, easyString: 'd' };
Key.KEY_E = { code: 'KeyE', keyCode: 69, easyString: 'e' };
Key.KEY_F = { code: 'KeyF', keyCode: 70, easyString: 'f' };
Key.KEY_G = { code: 'KeyG', keyCode: 71, easyString: 'g' };
Key.KEY_H = { code: 'KeyH', keyCode: 72, easyString: 'h' };
Key.KEY_I = { code: 'KeyI', keyCode: 73, easyString: 'i' };
Key.KEY_J = { code: 'KeyJ', keyCode: 74, easyString: 'j' };
Key.KEY_K = { code: 'KeyK', keyCode: 75, easyString: 'k' };
Key.KEY_L = { code: 'KeyL', keyCode: 76, easyString: 'l' };
Key.KEY_M = { code: 'KeyM', keyCode: 77, easyString: 'm' };
Key.KEY_N = { code: 'KeyN', keyCode: 78, easyString: 'n' };
Key.KEY_O = { code: 'KeyO', keyCode: 79, easyString: 'o' };
Key.KEY_P = { code: 'KeyP', keyCode: 80, easyString: 'p' };
Key.KEY_Q = { code: 'KeyQ', keyCode: 81, easyString: 'q' };
Key.KEY_R = { code: 'KeyR', keyCode: 82, easyString: 'r' };
Key.KEY_S = { code: 'KeyS', keyCode: 83, easyString: 's' };
Key.KEY_T = { code: 'KeyT', keyCode: 84, easyString: 't' };
Key.KEY_U = { code: 'KeyU', keyCode: 85, easyString: 'u' };
Key.KEY_V = { code: 'KeyV', keyCode: 86, easyString: 'v' };
Key.KEY_W = { code: 'KeyW', keyCode: 87, easyString: 'w' };
Key.KEY_X = { code: 'KeyX', keyCode: 88, easyString: 'x' };
Key.KEY_Y = { code: 'KeyY', keyCode: 89, easyString: 'y' };
Key.KEY_Z = { code: 'KeyZ', keyCode: 90, easyString: 'z' };
Key.MULTIPLY = { code: 'NumpadMultiply', keyCode: 106, easyString: 'multiply' };
Key.ADD = { code: 'NumpadAdd', keyCode: 107, easyString: 'add' };
Key.DECIMAL = { code: 'NumpadDecimal', keyCode: 108, easyString: 'decimal' };
Key.SUBTRACT = { code: 'NumpadSubtract', keyCode: 109, easyString: 'subtract' };
Key.DIVIDE = { code: 'NumpadDivide', keyCode: 111, easyString: 'divide' };
Key.F1 = { code: 'F1', keyCode: 112, easyString: 'f1' };
Key.F2 = { code: 'F2', keyCode: 113, easyString: 'f2' };
Key.F3 = { code: 'F3', keyCode: 114, easyString: 'f3' };
Key.F4 = { code: 'F4', keyCode: 115, easyString: 'f4' };
Key.F5 = { code: 'F5', keyCode: 116, easyString: 'f5' };
Key.F6 = { code: 'F6', keyCode: 117, easyString: 'f6' };
Key.F7 = { code: 'F7', keyCode: 118, easyString: 'f7' };
Key.F8 = { code: 'F8', keyCode: 119, easyString: 'f8' };
Key.F9 = { code: 'F9', keyCode: 120, easyString: 'f9' };
Key.F10 = { code: 'F10', keyCode: 121, easyString: 'f10' };
Key.F11 = { code: 'F11', keyCode: 122, easyString: 'f11' };
Key.F12 = { code: 'F12', keyCode: 123, easyString: 'f12' };
Key.F13 = { code: 'F13', keyCode: 124, easyString: 'f13' };
Key.F14 = { code: 'F14', keyCode: 125, easyString: 'f14' };
Key.F15 = { code: 'F15', keyCode: 126, easyString: 'f15' };
Key.F16 = { code: 'F16', keyCode: 127, easyString: 'f16' };
Key.F17 = { code: 'F17', keyCode: 128, easyString: 'f17' };
Key.F18 = { code: 'F18', keyCode: 129, easyString: 'f18' };
Key.F19 = { code: 'F19', keyCode: 130, easyString: 'f19' };
Key.F20 = { code: 'F20', keyCode: 131, easyString: 'f20' };
Key.F21 = { code: 'F21', keyCode: 132, easyString: 'f21' };
Key.F22 = { code: 'F22', keyCode: 133, easyString: 'f22' };
Key.F23 = { code: 'F23', keyCode: 134, easyString: 'f23' };
Key.F24 = { code: 'F24', keyCode: 135, easyString: 'f24' };
Key.NUM_LOCK = { code: 'NumLock', keyCode: 144, easyString: 'numlock' };
Key.SEMICOLON = { code: 'Semicolon', keyCode: 186, easyString: ';' };
Key.EQUAL = { code: 'Equal', keyCode: 187, easyString: '=' };
Key.COMMA = { code: 'Comma', keyCode: 188, easyString: ',' };
Key.MINUS = { code: 'Minus', keyCode: 189, easyString: '-' };
Key.PERIOD = { code: 'Period', keyCode: 190, easyString: '.' };
Key.SLASH = { code: 'Slash', keyCode: 191, easyString: '/' };
Key.BACKQUOTE = { code: 'Backquote', keyCode: 192, easyString: '`' };
Key.INTL_RO = { code: 'IntlRo', keyCode: 193, easyString: 'intlro' };
Key.BRACKET_LEFT = { code: 'BracketLeft', keyCode: 219, easyString: '[' };
Key.BACKSLASH = { code: 'Backslash', keyCode: 220, easyString: '\\' };
Key.BRACKET_RIGHT = { code: 'BracketRight', keyCode: 221, easyString: ']' };
Key.QUOTE = { code: 'Quote', keyCode: 222, easyString: '\'' };
Key.INTL_BACKSLASH = { code: 'IntlBackslash', keyCode: 229, easyString: 'intlbackslash' };
Key.INTL_YEN = { code: 'IntlYen', keyCode: 255, easyString: 'intlyen' };
Key.MAX_KEY_CODE = Key.INTL_YEN.keyCode;
})(Key || (Key = {}));
/* -------------------- Initialize the static key mappings -------------------- */
(() => {
// Set the default key mappings from the constants in the Key namespace
Object.keys(Key).map(prop => Reflect.get(Key, prop)).filter(key => Key.isKey(key)).forEach(key => {
CODE_TO_KEY[key.code] = key;
KEY_CODE_TO_KEY[key.keyCode] = key;
EASY_TO_KEY[key.easyString] = key;
});
// Set additional key mappings
CODE_TO_KEY['Numpad0'] = Key.DIGIT0;
KEY_CODE_TO_KEY[96] = Key.DIGIT0;
CODE_TO_KEY['Numpad1'] = Key.DIGIT1;
KEY_CODE_TO_KEY[97] = Key.DIGIT1;
CODE_TO_KEY['Numpad2'] = Key.DIGIT2;
KEY_CODE_TO_KEY[98] = Key.DIGIT2;
CODE_TO_KEY['Numpad3'] = Key.DIGIT3;
KEY_CODE_TO_KEY[99] = Key.DIGIT3;
CODE_TO_KEY['Numpad4'] = Key.DIGIT4;
KEY_CODE_TO_KEY[100] = Key.DIGIT4;
CODE_TO_KEY['Numpad5'] = Key.DIGIT5;
KEY_CODE_TO_KEY[101] = Key.DIGIT5;
CODE_TO_KEY['Numpad6'] = Key.DIGIT6;
KEY_CODE_TO_KEY[102] = Key.DIGIT6;
CODE_TO_KEY['Numpad7'] = Key.DIGIT7;
KEY_CODE_TO_KEY[103] = Key.DIGIT7;
CODE_TO_KEY['Numpad8'] = Key.DIGIT8;
KEY_CODE_TO_KEY[104] = Key.DIGIT8;
CODE_TO_KEY['Numpad9'] = Key.DIGIT9;
KEY_CODE_TO_KEY[105] = Key.DIGIT9;
CODE_TO_KEY['NumpadEnter'] = Key.ENTER;
CODE_TO_KEY['NumpadEqual'] = Key.EQUAL;
CODE_TO_KEY['MetaLeft'] = Key.OS_LEFT; // Chrome, Safari
KEY_CODE_TO_KEY[224] = Key.OS_LEFT; // Firefox on Mac
CODE_TO_KEY['MetaRight'] = Key.OS_RIGHT; // Chrome, Safari
KEY_CODE_TO_KEY[93] = Key.OS_RIGHT; // Chrome, Safari, Edge
KEY_CODE_TO_KEY[225] = Key.ALT_RIGHT; // Linux
KEY_CODE_TO_KEY[110] = Key.DECIMAL; // Mac, Windows
KEY_CODE_TO_KEY[59] = Key.SEMICOLON; // Firefox
KEY_CODE_TO_KEY[61] = Key.EQUAL; // Firefox
KEY_CODE_TO_KEY[173] = Key.MINUS; // Firefox
KEY_CODE_TO_KEY[226] = Key.BACKSLASH; // Chrome, Edge on Windows
KEY_CODE_TO_KEY[60] = Key.BACKSLASH; // Firefox on Linux
// Set the modifier keys
MODIFIERS.push(...[Key.ALT_LEFT, Key.ALT_RIGHT, Key.CONTROL_LEFT, Key.CONTROL_RIGHT, Key.OS_LEFT, Key.OS_RIGHT, Key.SHIFT_LEFT, Key.SHIFT_RIGHT]);
})();
export var KeysOrKeyCodes;
(function (KeysOrKeyCodes) {
KeysOrKeyCodes.toKeyCode = (keyOrKeyCode) => keyOrKeyCode instanceof KeyCode ? keyOrKeyCode : KeyCode.createKeyCode({ first: keyOrKeyCode });
KeysOrKeyCodes.toKeyCodes = (keysOrKeyCodes) => {
if (keysOrKeyCodes instanceof KeyCode) {
return [keysOrKeyCodes];
}
else if (Array.isArray(keysOrKeyCodes)) {
return keysOrKeyCodes.slice().map(KeysOrKeyCodes.toKeyCode);
}
return [KeysOrKeyCodes.toKeyCode(keysOrKeyCodes)];
};
})(KeysOrKeyCodes || (KeysOrKeyCodes = {}));
//# sourceMappingURL=keys.js.map