From a72dc40355bf3ab8e22bf79cd82c801decc92865 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Mon, 12 Sep 2016 21:51:34 +0200 Subject: [PATCH] mark KeyCode enum as const --- build/monaco/monaco.d.ts.recipe | 3 +- src/vs/base/browser/keyboardEvent.ts | 4 +- src/vs/base/common/keyCodes.ts | 247 +++++++++--------- src/vs/base/test/common/keyCodes.test.ts | 40 --- .../browser/standalone/standaloneEditor.ts | 2 +- .../common/standalone/standaloneBase.ts | 180 ++++++++++++- .../common/standalone/standaloneBase.test.ts | 170 ++++++++++++ src/vs/monaco.d.ts | 2 +- .../keybinding/common/keybindingsRegistry.ts | 4 +- .../electron-browser/nativeKeymap.ts | 4 +- 10 files changed, 483 insertions(+), 173 deletions(-) create mode 100644 src/vs/editor/test/common/standalone/standaloneBase.test.ts diff --git a/build/monaco/monaco.d.ts.recipe b/build/monaco/monaco.d.ts.recipe index af39c661697..9bc188ad22a 100644 --- a/build/monaco/monaco.d.ts.recipe +++ b/build/monaco/monaco.d.ts.recipe @@ -39,7 +39,8 @@ declare module monaco { #include(vs/base/common/winjs.base.d.ts): TValueCallback, ProgressCallback, TPromise #include(vs/base/common/cancellation): CancellationTokenSource, CancellationToken #include(vs/base/common/uri): URI -#include(vs/base/common/keyCodes): KeyCode, KeyMod +#include(vs/editor/common/standalone/standaloneBase): KeyCode +#include(vs/base/common/keyCodes): KeyMod #include(vs/base/common/htmlContent): MarkedString #include(vs/base/browser/keyboardEvent): IKeyboardEvent #include(vs/base/browser/mouseEvent): IMouseEvent diff --git a/src/vs/base/browser/keyboardEvent.ts b/src/vs/base/browser/keyboardEvent.ts index 1ea39065a5b..7fff9e0e27d 100644 --- a/src/vs/base/browser/keyboardEvent.ts +++ b/src/vs/base/browser/keyboardEvent.ts @@ -5,7 +5,7 @@ 'use strict'; -import {KeyCode, KeyMod} from 'vs/base/common/keyCodes'; +import {KeyCode, KeyCodeUtils, KeyMod} from 'vs/base/common/keyCodes'; import * as platform from 'vs/base/common/platform'; import * as browser from 'vs/base/browser/browser'; @@ -161,7 +161,7 @@ let extractKeyCode = function extractKeyCode(e:KeyboardEvent): KeyCode { if (e.charCode) { // "keypress" events mostly let char = String.fromCharCode(e.charCode).toUpperCase(); - return KeyCode.fromString(char); + return KeyCodeUtils.fromString(char); } return lookupKeyCode(e); }; diff --git a/src/vs/base/common/keyCodes.ts b/src/vs/base/common/keyCodes.ts index e76aabe5075..6492ab73f7f 100644 --- a/src/vs/base/common/keyCodes.ts +++ b/src/vs/base/common/keyCodes.ts @@ -19,181 +19,182 @@ export interface ISimplifiedPlatform { * Inspired somewhat from https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx * But these are "more general", as they should work across browsers & OS`s. */ -export enum KeyCode { +export const enum KeyCode { /** * Placed first to cover the 0 value of the enum. */ - Unknown, - - Backspace, - Tab, - Enter, - Shift, - Ctrl, - Alt, - PauseBreak, - CapsLock, - Escape, - Space, - PageUp, - PageDown, - End, - Home, - LeftArrow, - UpArrow, - RightArrow, - DownArrow, - Insert, - Delete, - - KEY_0, - KEY_1, - KEY_2, - KEY_3, - KEY_4, - KEY_5, - KEY_6, - KEY_7, - KEY_8, - KEY_9, - - KEY_A, - KEY_B, - KEY_C, - KEY_D, - KEY_E, - KEY_F, - KEY_G, - KEY_H, - KEY_I, - KEY_J, - KEY_K, - KEY_L, - KEY_M, - KEY_N, - KEY_O, - KEY_P, - KEY_Q, - KEY_R, - KEY_S, - KEY_T, - KEY_U, - KEY_V, - KEY_W, - KEY_X, - KEY_Y, - KEY_Z, - - Meta, - ContextMenu, - - F1, - F2, - F3, - F4, - F5, - F6, - F7, - F8, - F9, - F10, - F11, - F12, - F13, - F14, - F15, - F16, - F17, - F18, - F19, - - NumLock, - ScrollLock, + Unknown = 0, + + Backspace = 1, + Tab = 2, + Enter = 3, + Shift = 4, + Ctrl = 5, + Alt = 6, + PauseBreak = 7, + CapsLock = 8, + Escape = 9, + Space = 10, + PageUp = 11, + PageDown = 12, + End = 13, + Home = 14, + LeftArrow = 15, + UpArrow = 16, + RightArrow = 17, + DownArrow = 18, + Insert = 19, + Delete = 20, + + KEY_0 = 21, + KEY_1 = 22, + KEY_2 = 23, + KEY_3 = 24, + KEY_4 = 25, + KEY_5 = 26, + KEY_6 = 27, + KEY_7 = 28, + KEY_8 = 29, + KEY_9 = 30, + + KEY_A = 31, + KEY_B = 32, + KEY_C = 33, + KEY_D = 34, + KEY_E = 35, + KEY_F = 36, + KEY_G = 37, + KEY_H = 38, + KEY_I = 39, + KEY_J = 40, + KEY_K = 41, + KEY_L = 42, + KEY_M = 43, + KEY_N = 44, + KEY_O = 45, + KEY_P = 46, + KEY_Q = 47, + KEY_R = 48, + KEY_S = 49, + KEY_T = 50, + KEY_U = 51, + KEY_V = 52, + KEY_W = 53, + KEY_X = 54, + KEY_Y = 55, + KEY_Z = 56, + + Meta = 57, + ContextMenu = 58, + + F1 = 59, + F2 = 60, + F3 = 61, + F4 = 62, + F5 = 63, + F6 = 64, + F7 = 65, + F8 = 66, + F9 = 67, + F10 = 68, + F11 = 69, + F12 = 70, + F13 = 71, + F14 = 72, + F15 = 73, + F16 = 74, + F17 = 75, + F18 = 76, + F19 = 77, + + NumLock = 78, + ScrollLock = 79, /** * Used for miscellaneous characters; it can vary by keyboard. * For the US standard keyboard, the ';:' key */ - US_SEMICOLON, + US_SEMICOLON = 80, /** * For any country/region, the '+' key * For the US standard keyboard, the '=+' key */ - US_EQUAL, + US_EQUAL = 81, /** * For any country/region, the ',' key * For the US standard keyboard, the ',<' key */ - US_COMMA, + US_COMMA = 82, /** * For any country/region, the '-' key * For the US standard keyboard, the '-_' key */ - US_MINUS, + US_MINUS = 83, /** * For any country/region, the '.' key * For the US standard keyboard, the '.>' key */ - US_DOT, + US_DOT = 84, /** * Used for miscellaneous characters; it can vary by keyboard. * For the US standard keyboard, the '/?' key */ - US_SLASH, + US_SLASH = 85, /** * Used for miscellaneous characters; it can vary by keyboard. * For the US standard keyboard, the '`~' key */ - US_BACKTICK, + US_BACKTICK = 86, /** * Used for miscellaneous characters; it can vary by keyboard. * For the US standard keyboard, the '[{' key */ - US_OPEN_SQUARE_BRACKET, + US_OPEN_SQUARE_BRACKET = 87, /** * Used for miscellaneous characters; it can vary by keyboard. * For the US standard keyboard, the '\|' key */ - US_BACKSLASH, + US_BACKSLASH = 88, /** * Used for miscellaneous characters; it can vary by keyboard. * For the US standard keyboard, the ']}' key */ - US_CLOSE_SQUARE_BRACKET, + US_CLOSE_SQUARE_BRACKET = 89, /** * Used for miscellaneous characters; it can vary by keyboard. * For the US standard keyboard, the ''"' key */ - US_QUOTE, + US_QUOTE = 90, /** * Used for miscellaneous characters; it can vary by keyboard. */ - OEM_8, + OEM_8 = 91, /** * Either the angle bracket key or the backslash key on the RT 102-key keyboard. */ - OEM_102, - - NUMPAD_0, // VK_NUMPAD0, 0x60, Numeric keypad 0 key - NUMPAD_1, // VK_NUMPAD1, 0x61, Numeric keypad 1 key - NUMPAD_2, // VK_NUMPAD2, 0x62, Numeric keypad 2 key - NUMPAD_3, // VK_NUMPAD3, 0x63, Numeric keypad 3 key - NUMPAD_4, // VK_NUMPAD4, 0x64, Numeric keypad 4 key - NUMPAD_5, // VK_NUMPAD5, 0x65, Numeric keypad 5 key - NUMPAD_6, // VK_NUMPAD6, 0x66, Numeric keypad 6 key - NUMPAD_7, // VK_NUMPAD7, 0x67, Numeric keypad 7 key - NUMPAD_8, // VK_NUMPAD8, 0x68, Numeric keypad 8 key - NUMPAD_9, // VK_NUMPAD9, 0x69, Numeric keypad 9 key - - NUMPAD_MULTIPLY, // VK_MULTIPLY, 0x6A, Multiply key - NUMPAD_ADD, // VK_ADD, 0x6B, Add key - NUMPAD_SEPARATOR, // VK_SEPARATOR, 0x6C, Separator key - NUMPAD_SUBTRACT, // VK_SUBTRACT, 0x6D, Subtract key - NUMPAD_DECIMAL, // VK_DECIMAL, 0x6E, Decimal key - NUMPAD_DIVIDE, // VK_DIVIDE, 0x6F, + OEM_102 = 92, + + NUMPAD_0 = 93, // VK_NUMPAD0, 0x60, Numeric keypad 0 key + NUMPAD_1 = 94, // VK_NUMPAD1, 0x61, Numeric keypad 1 key + NUMPAD_2 = 95, // VK_NUMPAD2, 0x62, Numeric keypad 2 key + NUMPAD_3 = 96, // VK_NUMPAD3, 0x63, Numeric keypad 3 key + NUMPAD_4 = 97, // VK_NUMPAD4, 0x64, Numeric keypad 4 key + NUMPAD_5 = 98, // VK_NUMPAD5, 0x65, Numeric keypad 5 key + NUMPAD_6 = 99, // VK_NUMPAD6, 0x66, Numeric keypad 6 key + NUMPAD_7 = 100, // VK_NUMPAD7, 0x67, Numeric keypad 7 key + NUMPAD_8 = 101, // VK_NUMPAD8, 0x68, Numeric keypad 8 key + NUMPAD_9 = 102, // VK_NUMPAD9, 0x69, Numeric keypad 9 key + + NUMPAD_MULTIPLY = 103, // VK_MULTIPLY, 0x6A, Multiply key + NUMPAD_ADD = 104, // VK_ADD, 0x6B, Add key + NUMPAD_SEPARATOR = 105, // VK_SEPARATOR, 0x6C, Separator key + NUMPAD_SUBTRACT = 106, // VK_SUBTRACT, 0x6D, Subtract key + NUMPAD_DECIMAL = 107, // VK_DECIMAL, 0x6E, Decimal key + NUMPAD_DIVIDE = 108, // VK_DIVIDE, 0x6F, /** * Placed last to cover the length of the enum. + * Please do not depend on this value! */ MAX_VALUE } @@ -403,7 +404,7 @@ let USER_SETTINGS = createMapping((TO_USER_SETTINGS_MAP) => { FROM_USER_SETTINGS_MAP['OEM_102'] = KeyCode.OEM_102; }); -export namespace KeyCode { +export namespace KeyCodeUtils { export function toString(key:KeyCode): string { return STRING.fromKeyCode(key); } @@ -797,7 +798,7 @@ export class ElectronAcceleratorLabelProvider implements IKeyBindingLabelProvide return 'Right'; } - return KeyCode.toString(keyCode); + return KeyCodeUtils.toString(keyCode); } } @@ -831,7 +832,7 @@ export class MacUIKeyLabelProvider implements IKeyBindingLabelProvider { return MacUIKeyLabelProvider.downArrowUnicodeLabel; } - return KeyCode.toString(keyCode); + return KeyCodeUtils.toString(keyCode); } } @@ -849,7 +850,7 @@ export class AriaKeyLabelProvider implements IKeyBindingLabelProvider { public modifierSeparator = '+'; public getLabelForKey(keyCode:KeyCode): string { - return KeyCode.toString(keyCode); + return KeyCodeUtils.toString(keyCode); } } @@ -867,7 +868,7 @@ export class ClassicUIKeyLabelProvider implements IKeyBindingLabelProvider { public modifierSeparator = '+'; public getLabelForKey(keyCode:KeyCode): string { - return KeyCode.toString(keyCode); + return KeyCodeUtils.toString(keyCode); } } diff --git a/src/vs/base/test/common/keyCodes.test.ts b/src/vs/base/test/common/keyCodes.test.ts index 446a0a77b8d..d4f24a82972 100644 --- a/src/vs/base/test/common/keyCodes.test.ts +++ b/src/vs/base/test/common/keyCodes.test.ts @@ -65,44 +65,4 @@ suite('keyCodes', () => { assert.equal(encodedFirstPart, KeyMod.CtrlCmd | KeyCode.KEY_Y, 'first part'); assert.equal(encodedSecondPart, encodedSecondPart, 'chord part'); }); - - test('getUserSettingsKeybindingRegex', () => { - let regex = new RegExp(Keybinding.getUserSettingsKeybindingRegex()); - - function testIsGood(userSettingsLabel:string, message:string = userSettingsLabel): void { - let userSettings = '"' + userSettingsLabel.replace(/\\/g, '\\\\') + '"'; - let isGood = regex.test(userSettings); - assert.ok(isGood, message); - } - - // check that all key codes are covered by the regex - let ignore: boolean[] = []; - ignore[KeyCode.Shift] = true; - ignore[KeyCode.Ctrl] = true; - ignore[KeyCode.Alt] = true; - ignore[KeyCode.Meta] = true; - for (let keyCode = KeyCode.Unknown + 1; keyCode < KeyCode.MAX_VALUE; keyCode++) { - if (ignore[keyCode]) { - continue; - } - let userSettings = Keybinding.toUserSettingsLabel(keyCode); - testIsGood(userSettings, keyCode + ' - ' + KeyCode[keyCode] + ' - ' + userSettings); - } - - // one modifier - testIsGood('ctrl+a'); - testIsGood('shift+a'); - testIsGood('alt+a'); - testIsGood('cmd+a'); - testIsGood('meta+a'); - testIsGood('win+a'); - - // more modifiers - testIsGood('ctrl+shift+a'); - testIsGood('shift+alt+a'); - testIsGood('ctrl+shift+alt+a'); - - // chords - testIsGood('ctrl+a ctrl+a'); - }); }); diff --git a/src/vs/editor/browser/standalone/standaloneEditor.ts b/src/vs/editor/browser/standalone/standaloneEditor.ts index ab59657aa90..69cbbd351bc 100644 --- a/src/vs/editor/browser/standalone/standaloneEditor.ts +++ b/src/vs/editor/browser/standalone/standaloneEditor.ts @@ -243,7 +243,7 @@ export function colorizeModelLine(model:IModel, lineNumber:number, tabSize:numbe export function createMonacoEditorAPI(): typeof monaco.editor { return { // methods - create: create, + create: create, createDiffEditor: createDiffEditor, createDiffNavigator: createDiffNavigator, diff --git a/src/vs/editor/common/standalone/standaloneBase.ts b/src/vs/editor/common/standalone/standaloneBase.ts index 8827d4a8e83..b00252a52c5 100644 --- a/src/vs/editor/common/standalone/standaloneBase.ts +++ b/src/vs/editor/common/standalone/standaloneBase.ts @@ -5,7 +5,7 @@ 'use strict'; import {Emitter} from 'vs/base/common/event'; -import {KeyCode, KeyMod} from 'vs/base/common/keyCodes'; +import {KeyMod} from 'vs/base/common/keyCodes'; import {Position} from 'vs/editor/common/core/position'; import {Range} from 'vs/editor/common/core/range'; import {Selection, SelectionDirection} from 'vs/editor/common/core/selection'; @@ -14,6 +14,184 @@ import {CancellationTokenSource} from 'vs/base/common/cancellation'; import Severity from 'vs/base/common/severity'; import URI from 'vs/base/common/uri'; +// -------------------------------------------- +// This is repeated here so it can be exported +// -------------------------------------------- +/** + * Virtual Key Codes, the value does not hold any inherent meaning. + * Inspired somewhat from https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx + * But these are "more general", as they should work across browsers & OS`s. + */ +export enum KeyCode { + /** + * Placed first to cover the 0 value of the enum. + */ + Unknown = 0, + Backspace = 1, + Tab = 2, + Enter = 3, + Shift = 4, + Ctrl = 5, + Alt = 6, + PauseBreak = 7, + CapsLock = 8, + Escape = 9, + Space = 10, + PageUp = 11, + PageDown = 12, + End = 13, + Home = 14, + LeftArrow = 15, + UpArrow = 16, + RightArrow = 17, + DownArrow = 18, + Insert = 19, + Delete = 20, + KEY_0 = 21, + KEY_1 = 22, + KEY_2 = 23, + KEY_3 = 24, + KEY_4 = 25, + KEY_5 = 26, + KEY_6 = 27, + KEY_7 = 28, + KEY_8 = 29, + KEY_9 = 30, + KEY_A = 31, + KEY_B = 32, + KEY_C = 33, + KEY_D = 34, + KEY_E = 35, + KEY_F = 36, + KEY_G = 37, + KEY_H = 38, + KEY_I = 39, + KEY_J = 40, + KEY_K = 41, + KEY_L = 42, + KEY_M = 43, + KEY_N = 44, + KEY_O = 45, + KEY_P = 46, + KEY_Q = 47, + KEY_R = 48, + KEY_S = 49, + KEY_T = 50, + KEY_U = 51, + KEY_V = 52, + KEY_W = 53, + KEY_X = 54, + KEY_Y = 55, + KEY_Z = 56, + Meta = 57, + ContextMenu = 58, + F1 = 59, + F2 = 60, + F3 = 61, + F4 = 62, + F5 = 63, + F6 = 64, + F7 = 65, + F8 = 66, + F9 = 67, + F10 = 68, + F11 = 69, + F12 = 70, + F13 = 71, + F14 = 72, + F15 = 73, + F16 = 74, + F17 = 75, + F18 = 76, + F19 = 77, + NumLock = 78, + ScrollLock = 79, + /** + * Used for miscellaneous characters; it can vary by keyboard. + * For the US standard keyboard, the ';:' key + */ + US_SEMICOLON = 80, + /** + * For any country/region, the '+' key + * For the US standard keyboard, the '=+' key + */ + US_EQUAL = 81, + /** + * For any country/region, the ',' key + * For the US standard keyboard, the ',<' key + */ + US_COMMA = 82, + /** + * For any country/region, the '-' key + * For the US standard keyboard, the '-_' key + */ + US_MINUS = 83, + /** + * For any country/region, the '.' key + * For the US standard keyboard, the '.>' key + */ + US_DOT = 84, + /** + * Used for miscellaneous characters; it can vary by keyboard. + * For the US standard keyboard, the '/?' key + */ + US_SLASH = 85, + /** + * Used for miscellaneous characters; it can vary by keyboard. + * For the US standard keyboard, the '`~' key + */ + US_BACKTICK = 86, + /** + * Used for miscellaneous characters; it can vary by keyboard. + * For the US standard keyboard, the '[{' key + */ + US_OPEN_SQUARE_BRACKET = 87, + /** + * Used for miscellaneous characters; it can vary by keyboard. + * For the US standard keyboard, the '\|' key + */ + US_BACKSLASH = 88, + /** + * Used for miscellaneous characters; it can vary by keyboard. + * For the US standard keyboard, the ']}' key + */ + US_CLOSE_SQUARE_BRACKET = 89, + /** + * Used for miscellaneous characters; it can vary by keyboard. + * For the US standard keyboard, the ''"' key + */ + US_QUOTE = 90, + /** + * Used for miscellaneous characters; it can vary by keyboard. + */ + OEM_8 = 91, + /** + * Either the angle bracket key or the backslash key on the RT 102-key keyboard. + */ + OEM_102 = 92, + NUMPAD_0 = 93, + NUMPAD_1 = 94, + NUMPAD_2 = 95, + NUMPAD_3 = 96, + NUMPAD_4 = 97, + NUMPAD_5 = 98, + NUMPAD_6 = 99, + NUMPAD_7 = 100, + NUMPAD_8 = 101, + NUMPAD_9 = 102, + NUMPAD_MULTIPLY = 103, + NUMPAD_ADD = 104, + NUMPAD_SEPARATOR = 105, + NUMPAD_SUBTRACT = 106, + NUMPAD_DECIMAL = 107, + NUMPAD_DIVIDE = 108, + /** + * Placed last to cover the length of the enum. + * Please do not depend on this value! + */ + MAX_VALUE +} + export function createMonacoBaseAPI(): typeof monaco { return { editor: undefined, diff --git a/src/vs/editor/test/common/standalone/standaloneBase.test.ts b/src/vs/editor/test/common/standalone/standaloneBase.test.ts new file mode 100644 index 00000000000..4597467d3a5 --- /dev/null +++ b/src/vs/editor/test/common/standalone/standaloneBase.test.ts @@ -0,0 +1,170 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ +'use strict'; + +import * as assert from 'assert'; +import {KeyCode as StandaloneKeyCode} from 'vs/editor/common/standalone/standaloneBase'; +import {KeyCode as RuntimeKeyCode} from 'vs/base/common/keyCodes'; +import {/*KeyCode, KeyMod, BinaryKeybindings,*/ Keybinding} from 'vs/base/common/keyCodes'; + +suite('KeyCode', () => { + test('is exported correctly in standalone editor', () => { + + function assertKeyCode(standalone: StandaloneKeyCode, runtime: RuntimeKeyCode): void { + assert.equal(standalone, runtime); + } + + assertKeyCode(StandaloneKeyCode.Unknown, RuntimeKeyCode.Unknown); + assertKeyCode(StandaloneKeyCode.Backspace, RuntimeKeyCode.Backspace); + assertKeyCode(StandaloneKeyCode.Tab, RuntimeKeyCode.Tab); + assertKeyCode(StandaloneKeyCode.Enter, RuntimeKeyCode.Enter); + assertKeyCode(StandaloneKeyCode.Shift, RuntimeKeyCode.Shift); + assertKeyCode(StandaloneKeyCode.Ctrl, RuntimeKeyCode.Ctrl); + assertKeyCode(StandaloneKeyCode.Alt, RuntimeKeyCode.Alt); + assertKeyCode(StandaloneKeyCode.PauseBreak, RuntimeKeyCode.PauseBreak); + assertKeyCode(StandaloneKeyCode.CapsLock, RuntimeKeyCode.CapsLock); + assertKeyCode(StandaloneKeyCode.Escape, RuntimeKeyCode.Escape); + assertKeyCode(StandaloneKeyCode.Space, RuntimeKeyCode.Space); + assertKeyCode(StandaloneKeyCode.PageUp, RuntimeKeyCode.PageUp); + assertKeyCode(StandaloneKeyCode.PageDown, RuntimeKeyCode.PageDown); + assertKeyCode(StandaloneKeyCode.End, RuntimeKeyCode.End); + assertKeyCode(StandaloneKeyCode.Home, RuntimeKeyCode.Home); + assertKeyCode(StandaloneKeyCode.LeftArrow, RuntimeKeyCode.LeftArrow); + assertKeyCode(StandaloneKeyCode.UpArrow, RuntimeKeyCode.UpArrow); + assertKeyCode(StandaloneKeyCode.RightArrow, RuntimeKeyCode.RightArrow); + assertKeyCode(StandaloneKeyCode.DownArrow, RuntimeKeyCode.DownArrow); + assertKeyCode(StandaloneKeyCode.Insert, RuntimeKeyCode.Insert); + assertKeyCode(StandaloneKeyCode.Delete, RuntimeKeyCode.Delete); + assertKeyCode(StandaloneKeyCode.KEY_0, RuntimeKeyCode.KEY_0); + assertKeyCode(StandaloneKeyCode.KEY_1, RuntimeKeyCode.KEY_1); + assertKeyCode(StandaloneKeyCode.KEY_2, RuntimeKeyCode.KEY_2); + assertKeyCode(StandaloneKeyCode.KEY_3, RuntimeKeyCode.KEY_3); + assertKeyCode(StandaloneKeyCode.KEY_4, RuntimeKeyCode.KEY_4); + assertKeyCode(StandaloneKeyCode.KEY_5, RuntimeKeyCode.KEY_5); + assertKeyCode(StandaloneKeyCode.KEY_6, RuntimeKeyCode.KEY_6); + assertKeyCode(StandaloneKeyCode.KEY_7, RuntimeKeyCode.KEY_7); + assertKeyCode(StandaloneKeyCode.KEY_8, RuntimeKeyCode.KEY_8); + assertKeyCode(StandaloneKeyCode.KEY_9, RuntimeKeyCode.KEY_9); + assertKeyCode(StandaloneKeyCode.KEY_A, RuntimeKeyCode.KEY_A); + assertKeyCode(StandaloneKeyCode.KEY_B, RuntimeKeyCode.KEY_B); + assertKeyCode(StandaloneKeyCode.KEY_C, RuntimeKeyCode.KEY_C); + assertKeyCode(StandaloneKeyCode.KEY_D, RuntimeKeyCode.KEY_D); + assertKeyCode(StandaloneKeyCode.KEY_E, RuntimeKeyCode.KEY_E); + assertKeyCode(StandaloneKeyCode.KEY_F, RuntimeKeyCode.KEY_F); + assertKeyCode(StandaloneKeyCode.KEY_G, RuntimeKeyCode.KEY_G); + assertKeyCode(StandaloneKeyCode.KEY_H, RuntimeKeyCode.KEY_H); + assertKeyCode(StandaloneKeyCode.KEY_I, RuntimeKeyCode.KEY_I); + assertKeyCode(StandaloneKeyCode.KEY_J, RuntimeKeyCode.KEY_J); + assertKeyCode(StandaloneKeyCode.KEY_K, RuntimeKeyCode.KEY_K); + assertKeyCode(StandaloneKeyCode.KEY_L, RuntimeKeyCode.KEY_L); + assertKeyCode(StandaloneKeyCode.KEY_M, RuntimeKeyCode.KEY_M); + assertKeyCode(StandaloneKeyCode.KEY_N, RuntimeKeyCode.KEY_N); + assertKeyCode(StandaloneKeyCode.KEY_O, RuntimeKeyCode.KEY_O); + assertKeyCode(StandaloneKeyCode.KEY_P, RuntimeKeyCode.KEY_P); + assertKeyCode(StandaloneKeyCode.KEY_Q, RuntimeKeyCode.KEY_Q); + assertKeyCode(StandaloneKeyCode.KEY_R, RuntimeKeyCode.KEY_R); + assertKeyCode(StandaloneKeyCode.KEY_S, RuntimeKeyCode.KEY_S); + assertKeyCode(StandaloneKeyCode.KEY_T, RuntimeKeyCode.KEY_T); + assertKeyCode(StandaloneKeyCode.KEY_U, RuntimeKeyCode.KEY_U); + assertKeyCode(StandaloneKeyCode.KEY_V, RuntimeKeyCode.KEY_V); + assertKeyCode(StandaloneKeyCode.KEY_W, RuntimeKeyCode.KEY_W); + assertKeyCode(StandaloneKeyCode.KEY_X, RuntimeKeyCode.KEY_X); + assertKeyCode(StandaloneKeyCode.KEY_Y, RuntimeKeyCode.KEY_Y); + assertKeyCode(StandaloneKeyCode.KEY_Z, RuntimeKeyCode.KEY_Z); + assertKeyCode(StandaloneKeyCode.Meta, RuntimeKeyCode.Meta); + assertKeyCode(StandaloneKeyCode.ContextMenu, RuntimeKeyCode.ContextMenu); + assertKeyCode(StandaloneKeyCode.F1, RuntimeKeyCode.F1); + assertKeyCode(StandaloneKeyCode.F2, RuntimeKeyCode.F2); + assertKeyCode(StandaloneKeyCode.F3, RuntimeKeyCode.F3); + assertKeyCode(StandaloneKeyCode.F4, RuntimeKeyCode.F4); + assertKeyCode(StandaloneKeyCode.F5, RuntimeKeyCode.F5); + assertKeyCode(StandaloneKeyCode.F6, RuntimeKeyCode.F6); + assertKeyCode(StandaloneKeyCode.F7, RuntimeKeyCode.F7); + assertKeyCode(StandaloneKeyCode.F8, RuntimeKeyCode.F8); + assertKeyCode(StandaloneKeyCode.F9, RuntimeKeyCode.F9); + assertKeyCode(StandaloneKeyCode.F10, RuntimeKeyCode.F10); + assertKeyCode(StandaloneKeyCode.F11, RuntimeKeyCode.F11); + assertKeyCode(StandaloneKeyCode.F12, RuntimeKeyCode.F12); + assertKeyCode(StandaloneKeyCode.F13, RuntimeKeyCode.F13); + assertKeyCode(StandaloneKeyCode.F14, RuntimeKeyCode.F14); + assertKeyCode(StandaloneKeyCode.F15, RuntimeKeyCode.F15); + assertKeyCode(StandaloneKeyCode.F16, RuntimeKeyCode.F16); + assertKeyCode(StandaloneKeyCode.F17, RuntimeKeyCode.F17); + assertKeyCode(StandaloneKeyCode.F18, RuntimeKeyCode.F18); + assertKeyCode(StandaloneKeyCode.F19, RuntimeKeyCode.F19); + assertKeyCode(StandaloneKeyCode.NumLock, RuntimeKeyCode.NumLock); + assertKeyCode(StandaloneKeyCode.ScrollLock, RuntimeKeyCode.ScrollLock); + assertKeyCode(StandaloneKeyCode.US_SEMICOLON, RuntimeKeyCode.US_SEMICOLON); + assertKeyCode(StandaloneKeyCode.US_EQUAL, RuntimeKeyCode.US_EQUAL); + assertKeyCode(StandaloneKeyCode.US_COMMA, RuntimeKeyCode.US_COMMA); + assertKeyCode(StandaloneKeyCode.US_MINUS, RuntimeKeyCode.US_MINUS); + assertKeyCode(StandaloneKeyCode.US_DOT, RuntimeKeyCode.US_DOT); + assertKeyCode(StandaloneKeyCode.US_SLASH, RuntimeKeyCode.US_SLASH); + assertKeyCode(StandaloneKeyCode.US_BACKTICK, RuntimeKeyCode.US_BACKTICK); + assertKeyCode(StandaloneKeyCode.US_OPEN_SQUARE_BRACKET, RuntimeKeyCode.US_OPEN_SQUARE_BRACKET); + assertKeyCode(StandaloneKeyCode.US_BACKSLASH, RuntimeKeyCode.US_BACKSLASH); + assertKeyCode(StandaloneKeyCode.US_CLOSE_SQUARE_BRACKET, RuntimeKeyCode.US_CLOSE_SQUARE_BRACKET); + assertKeyCode(StandaloneKeyCode.US_QUOTE, RuntimeKeyCode.US_QUOTE); + assertKeyCode(StandaloneKeyCode.OEM_8, RuntimeKeyCode.OEM_8); + assertKeyCode(StandaloneKeyCode.OEM_102, RuntimeKeyCode.OEM_102); + assertKeyCode(StandaloneKeyCode.NUMPAD_0, RuntimeKeyCode.NUMPAD_0); + assertKeyCode(StandaloneKeyCode.NUMPAD_1, RuntimeKeyCode.NUMPAD_1); + assertKeyCode(StandaloneKeyCode.NUMPAD_2, RuntimeKeyCode.NUMPAD_2); + assertKeyCode(StandaloneKeyCode.NUMPAD_3, RuntimeKeyCode.NUMPAD_3); + assertKeyCode(StandaloneKeyCode.NUMPAD_4, RuntimeKeyCode.NUMPAD_4); + assertKeyCode(StandaloneKeyCode.NUMPAD_5, RuntimeKeyCode.NUMPAD_5); + assertKeyCode(StandaloneKeyCode.NUMPAD_6, RuntimeKeyCode.NUMPAD_6); + assertKeyCode(StandaloneKeyCode.NUMPAD_7, RuntimeKeyCode.NUMPAD_7); + assertKeyCode(StandaloneKeyCode.NUMPAD_8, RuntimeKeyCode.NUMPAD_8); + assertKeyCode(StandaloneKeyCode.NUMPAD_9, RuntimeKeyCode.NUMPAD_9); + assertKeyCode(StandaloneKeyCode.NUMPAD_MULTIPLY, RuntimeKeyCode.NUMPAD_MULTIPLY); + assertKeyCode(StandaloneKeyCode.NUMPAD_ADD, RuntimeKeyCode.NUMPAD_ADD); + assertKeyCode(StandaloneKeyCode.NUMPAD_SEPARATOR, RuntimeKeyCode.NUMPAD_SEPARATOR); + assertKeyCode(StandaloneKeyCode.NUMPAD_SUBTRACT, RuntimeKeyCode.NUMPAD_SUBTRACT); + assertKeyCode(StandaloneKeyCode.NUMPAD_DECIMAL, RuntimeKeyCode.NUMPAD_DECIMAL); + assertKeyCode(StandaloneKeyCode.NUMPAD_DIVIDE, RuntimeKeyCode.NUMPAD_DIVIDE); + assertKeyCode(StandaloneKeyCode.MAX_VALUE, RuntimeKeyCode.MAX_VALUE); + }); + + test('getUserSettingsKeybindingRegex', () => { + let regex = new RegExp(Keybinding.getUserSettingsKeybindingRegex()); + + function testIsGood(userSettingsLabel:string, message:string = userSettingsLabel): void { + let userSettings = '"' + userSettingsLabel.replace(/\\/g, '\\\\') + '"'; + let isGood = regex.test(userSettings); + assert.ok(isGood, message); + } + + // check that all key codes are covered by the regex + let ignore: boolean[] = []; + ignore[RuntimeKeyCode.Shift] = true; + ignore[RuntimeKeyCode.Ctrl] = true; + ignore[RuntimeKeyCode.Alt] = true; + ignore[RuntimeKeyCode.Meta] = true; + for (let keyCode = RuntimeKeyCode.Unknown + 1; keyCode < RuntimeKeyCode.MAX_VALUE; keyCode++) { + if (ignore[keyCode]) { + continue; + } + let userSettings = Keybinding.toUserSettingsLabel(keyCode); + testIsGood(userSettings, keyCode + ' - ' + StandaloneKeyCode[keyCode] + ' - ' + userSettings); + } + + // one modifier + testIsGood('ctrl+a'); + testIsGood('shift+a'); + testIsGood('alt+a'); + testIsGood('cmd+a'); + testIsGood('meta+a'); + testIsGood('win+a'); + + // more modifiers + testIsGood('ctrl+shift+a'); + testIsGood('shift+alt+a'); + testIsGood('ctrl+shift+alt+a'); + + // chords + testIsGood('ctrl+a ctrl+a'); + }); +}); \ No newline at end of file diff --git a/src/vs/monaco.d.ts b/src/vs/monaco.d.ts index 28685b5daa9..cd26f6d2373 100644 --- a/src/vs/monaco.d.ts +++ b/src/vs/monaco.d.ts @@ -170,7 +170,6 @@ declare module monaco { toJSON(): any; static revive(data: any): Uri; } - /** * Virtual Key Codes, the value does not hold any inherent meaning. * Inspired somewhat from https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx @@ -341,6 +340,7 @@ declare module monaco { NUMPAD_DIVIDE = 108, /** * Placed last to cover the length of the enum. + * Please do not depend on this value! */ MAX_VALUE = 109, } diff --git a/src/vs/platform/keybinding/common/keybindingsRegistry.ts b/src/vs/platform/keybinding/common/keybindingsRegistry.ts index 19317b58b28..ef24bcb6feb 100644 --- a/src/vs/platform/keybinding/common/keybindingsRegistry.ts +++ b/src/vs/platform/keybinding/common/keybindingsRegistry.ts @@ -4,7 +4,7 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; -import {BinaryKeybindings, KeyCode} from 'vs/base/common/keyCodes'; +import {BinaryKeybindings, KeyCodeUtils} from 'vs/base/common/keyCodes'; import * as platform from 'vs/base/common/platform'; import {IKeybindingItem, IKeybindings} from 'vs/platform/keybinding/common/keybinding'; import {ContextKeyExpr} from 'vs/platform/contextkey/common/contextkey'; @@ -105,7 +105,7 @@ class KeybindingsRegistryImpl implements IKeybindingsRegistry { private registerDefaultKeybinding(keybinding: number, commandId: string, weight1: number, weight2: number, when: ContextKeyExpr): void { if (platform.isWindows) { if (BinaryKeybindings.hasCtrlCmd(keybinding) && !BinaryKeybindings.hasShift(keybinding) && BinaryKeybindings.hasAlt(keybinding) && !BinaryKeybindings.hasWinCtrl(keybinding)) { - if (/^[A-Z0-9\[\]\|\;\'\,\.\/\`]$/.test(KeyCode.toString(BinaryKeybindings.extractKeyCode(keybinding)))) { + if (/^[A-Z0-9\[\]\|\;\'\,\.\/\`]$/.test(KeyCodeUtils.toString(BinaryKeybindings.extractKeyCode(keybinding)))) { console.warn('Ctrl+Alt+ keybindings should not be used by default under Windows. Offender: ', keybinding, ' for ', commandId); } } diff --git a/src/vs/workbench/services/keybinding/electron-browser/nativeKeymap.ts b/src/vs/workbench/services/keybinding/electron-browser/nativeKeymap.ts index 77b2d0f8967..7a61e2e1d2f 100644 --- a/src/vs/workbench/services/keybinding/electron-browser/nativeKeymap.ts +++ b/src/vs/workbench/services/keybinding/electron-browser/nativeKeymap.ts @@ -5,7 +5,7 @@ 'use strict'; import * as nativeKeymap from 'native-keymap'; -import {KeyCode, IKeyBindingLabelProvider, MacUIKeyLabelProvider, ClassicUIKeyLabelProvider, AriaKeyLabelProvider} from 'vs/base/common/keyCodes'; +import {KeyCode, KeyCodeUtils, IKeyBindingLabelProvider, MacUIKeyLabelProvider, ClassicUIKeyLabelProvider, AriaKeyLabelProvider} from 'vs/base/common/keyCodes'; import {lookupKeyCode, setExtractKeyCode} from 'vs/base/browser/keyboardEvent'; import Platform = require('vs/base/common/platform'); @@ -308,7 +308,7 @@ setExtractKeyCode((e:KeyboardEvent) => { if (e.charCode) { // "keypress" events mostly let char = String.fromCharCode(e.charCode).toUpperCase(); - return KeyCode.fromString(char); + return KeyCodeUtils.fromString(char); } if (Platform.isMacintosh && _b24_interestingVirtualKeyCodes[e.keyCode] && typeof (e).keyIdentifier === 'string') { -- GitLab