keyCodes.ts 21.8 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

"use strict";

A
Alex Dima 已提交
8 9
import * as nls from 'vs/nls';
import * as defaultPlatform from 'vs/base/common/platform';
10
import {IHTMLContentElement} from 'vs/base/common/htmlContent';
E
Erich Gamma 已提交
11

A
Alex Dima 已提交
12 13 14 15 16
export interface ISimplifiedPlatform {
	isMacintosh: boolean;
	isWindows: boolean;
}

E
Erich Gamma 已提交
17 18 19
/**
 * 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
20
 * But these are "more general", as they should work across browsers & OS`s.
E
Erich Gamma 已提交
21 22 23
 */
export enum KeyCode {
	/**
24
	 * Placed first to cover the 0 value of the enum.
E
Erich Gamma 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	 */
	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,
102 103 104 105 106 107 108
	F13,
	F14,
	F15,
	F16,
	F17,
	F18,
	F19,
E
Erich Gamma 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

	NumLock,
	ScrollLock,

	/**
	 * For the US standard keyboard, the ';:' key
	 */
	US_SEMICOLON,
	/**
	 * For the US standard keyboard, the '=+' key
	 */
	US_EQUAL,
	/**
	 * For the US standard keyboard, the ',<' key
	 */
	US_COMMA,
	/**
	 * For the US standard keyboard, the '-_' key
	 */
	US_MINUS,
	/**
	 * For the US standard keyboard, the '.>' key
	 */
	US_DOT,
	/**
	 * For the US standard keyboard, the '/?' key
	 */
	US_SLASH,
	/**
	 * For the US standard keyboard, the '`~' key
	 */
	US_BACKTICK,
	/**
	 * For the US standard keyboard, the '[{' key
	 */
	US_OPEN_SQUARE_BRACKET,
	/**
	 * For the US standard keyboard, the '\|' key
	 */
	US_BACKSLASH,
	/**
	 * For the US standard keyboard, the ']}' key
	 */
	US_CLOSE_SQUARE_BRACKET,
	/**
	 * For the US standard keyboard, the ''"' key
	 */
156 157
	US_QUOTE,

158 159 160 161 162 163 164 165 166 167 168 169 170
	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
171
	NUMPAD_SEPARATOR,	// VK_SEPARATOR, 0x6C, Separator key
172 173 174 175
	NUMPAD_SUBTRACT,	// VK_SUBTRACT, 0x6D, Subtract key
	NUMPAD_DECIMAL,		// VK_DECIMAL, 0x6E, Decimal key
	NUMPAD_DIVIDE,		// VK_DIVIDE, 0x6F,

176 177 178 179
	/**
	 * Placed last to cover the length of the enum.
	 */
	MAX_VALUE
E
Erich Gamma 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
}

let TO_STRING_MAP: string[] = [];
(function() {
	TO_STRING_MAP[KeyCode.Unknown] 		= 'unknown';

	TO_STRING_MAP[KeyCode.Backspace] 	= 'Backspace';
	TO_STRING_MAP[KeyCode.Tab] 			= 'Tab';
	TO_STRING_MAP[KeyCode.Enter] 		= 'Enter';
	TO_STRING_MAP[KeyCode.Shift] 		= 'Shift';
	TO_STRING_MAP[KeyCode.Ctrl] 		= 'Ctrl';
	TO_STRING_MAP[KeyCode.Alt] 			= 'Alt';
	TO_STRING_MAP[KeyCode.PauseBreak] 	= 'PauseBreak';
	TO_STRING_MAP[KeyCode.CapsLock] 	= 'CapsLock';
	TO_STRING_MAP[KeyCode.Escape] 		= 'Escape';
	TO_STRING_MAP[KeyCode.Space] 		= 'Space';
	TO_STRING_MAP[KeyCode.PageUp] 		= 'PageUp';
	TO_STRING_MAP[KeyCode.PageDown] 	= 'PageDown';
	TO_STRING_MAP[KeyCode.End] 			= 'End';
	TO_STRING_MAP[KeyCode.Home] 		= 'Home';
	TO_STRING_MAP[KeyCode.LeftArrow] 	= 'LeftArrow';
	TO_STRING_MAP[KeyCode.UpArrow] 		= 'UpArrow';
	TO_STRING_MAP[KeyCode.RightArrow] 	= 'RightArrow';
	TO_STRING_MAP[KeyCode.DownArrow] 	= 'DownArrow';
	TO_STRING_MAP[KeyCode.Insert] 		= 'Insert';
	TO_STRING_MAP[KeyCode.Delete] 		= 'Delete';

	TO_STRING_MAP[KeyCode.KEY_0] = '0';
	TO_STRING_MAP[KeyCode.KEY_1] = '1';
	TO_STRING_MAP[KeyCode.KEY_2] = '2';
	TO_STRING_MAP[KeyCode.KEY_3] = '3';
	TO_STRING_MAP[KeyCode.KEY_4] = '4';
	TO_STRING_MAP[KeyCode.KEY_5] = '5';
	TO_STRING_MAP[KeyCode.KEY_6] = '6';
	TO_STRING_MAP[KeyCode.KEY_7] = '7';
	TO_STRING_MAP[KeyCode.KEY_8] = '8';
	TO_STRING_MAP[KeyCode.KEY_9] = '9';

	TO_STRING_MAP[KeyCode.KEY_A] = 'A';
	TO_STRING_MAP[KeyCode.KEY_B] = 'B';
	TO_STRING_MAP[KeyCode.KEY_C] = 'C';
	TO_STRING_MAP[KeyCode.KEY_D] = 'D';
	TO_STRING_MAP[KeyCode.KEY_E] = 'E';
	TO_STRING_MAP[KeyCode.KEY_F] = 'F';
	TO_STRING_MAP[KeyCode.KEY_G] = 'G';
	TO_STRING_MAP[KeyCode.KEY_H] = 'H';
	TO_STRING_MAP[KeyCode.KEY_I] = 'I';
	TO_STRING_MAP[KeyCode.KEY_J] = 'J';
	TO_STRING_MAP[KeyCode.KEY_K] = 'K';
	TO_STRING_MAP[KeyCode.KEY_L] = 'L';
	TO_STRING_MAP[KeyCode.KEY_M] = 'M';
	TO_STRING_MAP[KeyCode.KEY_N] = 'N';
	TO_STRING_MAP[KeyCode.KEY_O] = 'O';
	TO_STRING_MAP[KeyCode.KEY_P] = 'P';
	TO_STRING_MAP[KeyCode.KEY_Q] = 'Q';
	TO_STRING_MAP[KeyCode.KEY_R] = 'R';
	TO_STRING_MAP[KeyCode.KEY_S] = 'S';
	TO_STRING_MAP[KeyCode.KEY_T] = 'T';
	TO_STRING_MAP[KeyCode.KEY_U] = 'U';
	TO_STRING_MAP[KeyCode.KEY_V] = 'V';
	TO_STRING_MAP[KeyCode.KEY_W] = 'W';
	TO_STRING_MAP[KeyCode.KEY_X] = 'X';
	TO_STRING_MAP[KeyCode.KEY_Y] = 'Y';
	TO_STRING_MAP[KeyCode.KEY_Z] = 'Z';

	TO_STRING_MAP[KeyCode.ContextMenu] = 'ContextMenu';

	TO_STRING_MAP[KeyCode.F1] = 'F1';
	TO_STRING_MAP[KeyCode.F2] = 'F2';
	TO_STRING_MAP[KeyCode.F3] = 'F3';
	TO_STRING_MAP[KeyCode.F4] = 'F4';
	TO_STRING_MAP[KeyCode.F5] = 'F5';
	TO_STRING_MAP[KeyCode.F6] = 'F6';
	TO_STRING_MAP[KeyCode.F7] = 'F7';
	TO_STRING_MAP[KeyCode.F8] = 'F8';
	TO_STRING_MAP[KeyCode.F9] = 'F9';
	TO_STRING_MAP[KeyCode.F10] = 'F10';
	TO_STRING_MAP[KeyCode.F11] = 'F11';
	TO_STRING_MAP[KeyCode.F12] = 'F12';
259 260 261 262 263 264 265 266
	TO_STRING_MAP[KeyCode.F13] = 'F13';
	TO_STRING_MAP[KeyCode.F14] = 'F14';
	TO_STRING_MAP[KeyCode.F15] = 'F15';
	TO_STRING_MAP[KeyCode.F16] = 'F16';
	TO_STRING_MAP[KeyCode.F17] = 'F17';
	TO_STRING_MAP[KeyCode.F18] = 'F18';
	TO_STRING_MAP[KeyCode.F19] = 'F19';

E
Erich Gamma 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

	TO_STRING_MAP[KeyCode.NumLock] 		= 'NumLock';
	TO_STRING_MAP[KeyCode.ScrollLock] 	= 'ScrollLock';

	TO_STRING_MAP[KeyCode.US_SEMICOLON] 			= ';';
	TO_STRING_MAP[KeyCode.US_EQUAL] 				= '=';
	TO_STRING_MAP[KeyCode.US_COMMA] 				= ',';
	TO_STRING_MAP[KeyCode.US_MINUS] 				= '-';
	TO_STRING_MAP[KeyCode.US_DOT] 					= '.';
	TO_STRING_MAP[KeyCode.US_SLASH] 				= '/';
	TO_STRING_MAP[KeyCode.US_BACKTICK] 				= '`';
	TO_STRING_MAP[KeyCode.US_OPEN_SQUARE_BRACKET] 	= '[';
	TO_STRING_MAP[KeyCode.US_BACKSLASH] 			= '\\';
	TO_STRING_MAP[KeyCode.US_CLOSE_SQUARE_BRACKET] 	= ']';
	TO_STRING_MAP[KeyCode.US_QUOTE]					= '\'';
282 283 284 285 286 287 288 289 290 291 292 293 294 295

	TO_STRING_MAP[KeyCode.NUMPAD_0] = 'NumPad0';
	TO_STRING_MAP[KeyCode.NUMPAD_1] = 'NumPad1';
	TO_STRING_MAP[KeyCode.NUMPAD_2] = 'NumPad2';
	TO_STRING_MAP[KeyCode.NUMPAD_3] = 'NumPad3';
	TO_STRING_MAP[KeyCode.NUMPAD_4] = 'NumPad4';
	TO_STRING_MAP[KeyCode.NUMPAD_5] = 'NumPad5';
	TO_STRING_MAP[KeyCode.NUMPAD_6] = 'NumPad6';
	TO_STRING_MAP[KeyCode.NUMPAD_7] = 'NumPad7';
	TO_STRING_MAP[KeyCode.NUMPAD_8] = 'NumPad8';
	TO_STRING_MAP[KeyCode.NUMPAD_9] = 'NumPad9';

	TO_STRING_MAP[KeyCode.NUMPAD_MULTIPLY] = 'NumPad_Multiply';
	TO_STRING_MAP[KeyCode.NUMPAD_ADD] = 'NumPad_Add';
296
	TO_STRING_MAP[KeyCode.NUMPAD_SEPARATOR] = 'NumPad_Separator';
297 298 299 300 301 302 303 304 305
	TO_STRING_MAP[KeyCode.NUMPAD_SUBTRACT] = 'NumPad_Subtract';
	TO_STRING_MAP[KeyCode.NUMPAD_DECIMAL] = 'NumPad_Decimal';
	TO_STRING_MAP[KeyCode.NUMPAD_DIVIDE] = 'NumPad_Divide';

	// for (let i = 0; i < KeyCode.MAX_VALUE; i++) {
	// 	if (!TO_STRING_MAP[i]) {
	// 		console.warn('Missing string representation for ' + KeyCode[i]);
	// 	}
	// }
E
Erich Gamma 已提交
306 307 308 309 310 311
})();

let FROM_STRING_MAP: {[str:string]:KeyCode;} = {};
FROM_STRING_MAP['\r'] = KeyCode.Enter;
(function() {
	for (let i = 0, len = TO_STRING_MAP.length; i < len; i++) {
312 313 314
		if (!TO_STRING_MAP[i]) {
			continue;
		}
E
Erich Gamma 已提交
315
		FROM_STRING_MAP[TO_STRING_MAP[i]] = i;
316
		FROM_STRING_MAP[TO_STRING_MAP[i].toLowerCase()] = i;
E
Erich Gamma 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
	}
})();

export namespace KeyCode {
	export function toString(key:KeyCode): string {
		return TO_STRING_MAP[key];
	}
	export function fromString(key:string): KeyCode {
		if (FROM_STRING_MAP.hasOwnProperty(key)) {
			return FROM_STRING_MAP[key];
		}
		return KeyCode.Unknown;
	}
}

// Binary encoding strategy:
// 15:  1 bit for ctrlCmd
// 14:  1 bit for shift
// 13:  1 bit for alt
// 12:  1 bit for winCtrl
//  0: 12 bits for keyCode (up to a maximum keyCode of 4096. Given we have 83 at this point thats good enough)

const BIN_CTRLCMD_MASK = 1 << 15;
const BIN_SHIFT_MASK = 1 << 14;
const BIN_ALT_MASK = 1 << 13;
const BIN_WINCTRL_MASK = 1 << 12;
const BIN_KEYCODE_MASK = 0x00000fff;

export class BinaryKeybindings {

	public static extractFirstPart(keybinding:number): number {
		return keybinding & 0x0000ffff;
	}

	public static extractChordPart(keybinding:number): number {
		return (keybinding >> 16) & 0x0000ffff;
	}

	public static hasChord(keybinding:number): boolean {
		return (this.extractChordPart(keybinding) !== 0);
	}

	public static hasCtrlCmd(keybinding:number): boolean {
		return (keybinding & BIN_CTRLCMD_MASK ? true : false);
	}

	public static hasShift(keybinding:number): boolean {
		return (keybinding & BIN_SHIFT_MASK ? true : false);
	}

	public static hasAlt(keybinding:number): boolean {
		return (keybinding & BIN_ALT_MASK ? true : false);
	}

	public static hasWinCtrl(keybinding:number): boolean {
		return (keybinding & BIN_WINCTRL_MASK ? true : false);
	}

	public static extractKeyCode(keybinding:number): KeyCode {
		return (keybinding & BIN_KEYCODE_MASK);
	}
}



export class KeyMod {
	public static CtrlCmd = BIN_CTRLCMD_MASK;
	public static Shift = BIN_SHIFT_MASK;
	public static Alt = BIN_ALT_MASK;
	public static WinCtrl = BIN_WINCTRL_MASK;

	public static chord(firstPart:number, secondPart:number): number {
		return firstPart | ((secondPart & 0x0000ffff) << 16);
	}
}

/**
 * A set of usual keybindings that can be reused in code
 */
export class CommonKeybindings {

	public static ENTER: number = KeyCode.Enter;
	public static SHIFT_ENTER: number = KeyMod.Shift | KeyCode.Enter;
	public static CTRLCMD_ENTER: number = KeyMod.CtrlCmd | KeyCode.Enter;
	public static WINCTRL_ENTER: number = KeyMod.WinCtrl | KeyCode.Enter;

	public static TAB: number = KeyCode.Tab;
	public static ESCAPE: number = KeyCode.Escape;
	public static SPACE: number = KeyCode.Space;
	public static DELETE: number = KeyCode.Delete;
	public static SHIFT_DELETE: number = KeyMod.Shift | KeyCode.Delete;
	public static CTRLCMD_BACKSPACE: number = KeyMod.CtrlCmd | KeyCode.Backspace;

	public static UP_ARROW: number = KeyCode.UpArrow;
	public static SHIFT_UP_ARROW: number = KeyMod.Shift | KeyCode.UpArrow;
	public static CTRLCMD_UP_ARROW: number = KeyMod.CtrlCmd | KeyCode.UpArrow;

	public static DOWN_ARROW: number = KeyCode.DownArrow;
	public static SHIFT_DOWN_ARROW: number = KeyMod.Shift | KeyCode.DownArrow;
	public static CTRLCMD_DOWN_ARROW: number = KeyMod.CtrlCmd | KeyCode.DownArrow;

	public static LEFT_ARROW: number = KeyCode.LeftArrow;

	public static RIGHT_ARROW: number = KeyCode.RightArrow;

	public static PAGE_UP: number = KeyCode.PageUp;
	public static SHIFT_PAGE_UP: number = KeyMod.Shift | KeyCode.PageUp;

	public static PAGE_DOWN: number = KeyCode.PageDown;
	public static SHIFT_PAGE_DOWN: number = KeyMod.Shift | KeyCode.PageDown;

	public static F2: number = KeyCode.F2;

	public static CTRLCMD_S: number = KeyMod.CtrlCmd | KeyCode.KEY_S;
	public static CTRLCMD_C: number = KeyMod.CtrlCmd | KeyCode.KEY_C;
	public static CTRLCMD_V: number = KeyMod.CtrlCmd | KeyCode.KEY_V;
}

export class Keybinding {

	/**
	 * Format the binding to a format appropiate for rendering in the UI
	 */
A
Alex Dima 已提交
440 441
	private static _toUSLabel(value:number, Platform:ISimplifiedPlatform): string {
		return _asString(value, (Platform.isMacintosh ? MacUIKeyLabelProvider.INSTANCE : ClassicUIKeyLabelProvider.INSTANCE), Platform);
E
Erich Gamma 已提交
442 443
	}

444 445 446
	/**
	 * Format the binding to a format appropiate for rendering in the UI
	 */
A
Alex Dima 已提交
447 448
	private static _toUSHTMLLabel(value:number, Platform:ISimplifiedPlatform): IHTMLContentElement[] {
		return _asHTML(value, (Platform.isMacintosh ? MacUIKeyLabelProvider.INSTANCE : ClassicUIKeyLabelProvider.INSTANCE), Platform);
449 450
	}

451 452 453
	/**
	 * Format the binding to a format appropiate for rendering in the UI
	 */
A
Alex Dima 已提交
454 455
	private static _toCustomLabel(value:number, labelProvider:IKeyBindingLabelProvider, Platform:ISimplifiedPlatform): string {
		return _asString(value, labelProvider, Platform);
456 457
	}

458 459 460
	/**
	 * Format the binding to a format appropiate for rendering in the UI
	 */
A
Alex Dima 已提交
461 462
	private static _toCustomHTMLLabel(value:number, labelProvider:IKeyBindingLabelProvider, Platform:ISimplifiedPlatform): IHTMLContentElement[] {
		return _asHTML(value, labelProvider, Platform);
463 464
	}

E
Erich Gamma 已提交
465 466 467 468
	/**
	 * This prints the binding in a format suitable for electron's accelerators.
	 * See https://github.com/atom/electron/blob/master/docs/api/accelerator.md
	 */
A
Alex Dima 已提交
469
	private static _toElectronAccelerator(value:number, Platform:ISimplifiedPlatform): string {
E
Erich Gamma 已提交
470 471 472 473
		if (BinaryKeybindings.hasChord(value)) {
			// Electron cannot handle chords
			return null;
		}
A
Alex Dima 已提交
474
		return _asString(value, ElectronAcceleratorLabelProvider.INSTANCE, Platform);
E
Erich Gamma 已提交
475 476 477 478 479
	}

	/**
	 * Format the binding to a format appropiate for the user settings file.
	 */
A
Alex Dima 已提交
480 481
	public static toUserSettingsLabel(value:number, Platform:ISimplifiedPlatform = defaultPlatform): string {
		let result = _asString(value, UserSettingsKeyLabelProvider.INSTANCE, Platform);
482 483 484 485 486 487 488 489 490
		result = result.toLowerCase().replace(/arrow/g, '');

		if (Platform.isMacintosh) {
			result = result.replace(/meta/g, 'cmd');
		} else if (Platform.isWindows) {
			result = result.replace(/meta/g, 'win');
		}

		return result;
E
Erich Gamma 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
	}

	public value:number;

	constructor(keybinding:number) {
		this.value = keybinding;
	}

	public hasCtrlCmd(): boolean {
		return BinaryKeybindings.hasCtrlCmd(this.value);
	}

	public hasShift(): boolean {
		return BinaryKeybindings.hasShift(this.value);
	}

	public hasAlt(): boolean {
		return BinaryKeybindings.hasAlt(this.value);
	}

	public hasWinCtrl(): boolean {
		return BinaryKeybindings.hasWinCtrl(this.value);
	}

	public extractKeyCode(): KeyCode {
		return BinaryKeybindings.extractKeyCode(this.value);
	}

	/**
	 * Format the binding to a format appropiate for rendering in the UI
	 */
A
Alex Dima 已提交
522 523
	public _toUSLabel(Platform:ISimplifiedPlatform = defaultPlatform): string {
		return Keybinding._toUSLabel(this.value, Platform);
524 525
	}

526 527 528
	/**
	 * Format the binding to a format appropiate for rendering in the UI
	 */
A
Alex Dima 已提交
529 530
	public _toUSHTMLLabel(Platform:ISimplifiedPlatform = defaultPlatform): IHTMLContentElement[] {
		return Keybinding._toUSHTMLLabel(this.value, Platform);
531 532
	}

533 534 535
	/**
	 * Format the binding to a format appropiate for rendering in the UI
	 */
A
Alex Dima 已提交
536 537
	public toCustomLabel(labelProvider:IKeyBindingLabelProvider, Platform:ISimplifiedPlatform = defaultPlatform): string {
		return Keybinding._toCustomLabel(this.value, labelProvider, Platform);
E
Erich Gamma 已提交
538 539
	}

540 541 542
	/**
	 * Format the binding to a format appropiate for rendering in the UI
	 */
A
Alex Dima 已提交
543 544
	public toCustomHTMLLabel(labelProvider:IKeyBindingLabelProvider, Platform:ISimplifiedPlatform = defaultPlatform): IHTMLContentElement[] {
		return Keybinding._toCustomHTMLLabel(this.value, labelProvider, Platform);
545 546
	}

E
Erich Gamma 已提交
547 548 549 550
	/**
	 * This prints the binding in a format suitable for electron's accelerators.
	 * See https://github.com/atom/electron/blob/master/docs/api/accelerator.md
	 */
A
Alex Dima 已提交
551 552
	public _toElectronAccelerator(Platform:ISimplifiedPlatform = defaultPlatform): string {
		return Keybinding._toElectronAccelerator(this.value, Platform);
E
Erich Gamma 已提交
553 554 555 556 557
	}

	/**
	 * Format the binding to a format appropiate for the user settings file.
	 */
A
Alex Dima 已提交
558 559
	public toUserSettingsLabel(Platform:ISimplifiedPlatform = defaultPlatform): string {
		return Keybinding.toUserSettingsLabel(this.value, Platform);
E
Erich Gamma 已提交
560 561 562 563
	}

}

564
export interface IKeyBindingLabelProvider {
E
Erich Gamma 已提交
565 566 567 568 569 570 571 572 573 574 575 576
	ctrlKeyLabel:string;
	shiftKeyLabel:string;
	altKeyLabel:string;
	cmdKeyLabel:string;
	windowsKeyLabel:string;
	modifierSeparator:string;
	getLabelForKey(keyCode:KeyCode): string;
}

/**
 * Print for Electron
 */
577
export class ElectronAcceleratorLabelProvider implements IKeyBindingLabelProvider {
E
Erich Gamma 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
	public static INSTANCE = new ElectronAcceleratorLabelProvider();

	public ctrlKeyLabel = 'Ctrl';
	public shiftKeyLabel = 'Shift';
	public altKeyLabel = 'Alt';
	public cmdKeyLabel = 'Cmd';
	public windowsKeyLabel = 'Super';
	public modifierSeparator = '+';

	public getLabelForKey(keyCode:KeyCode): string {
		switch (keyCode) {
			case KeyCode.UpArrow:
				return 'Up';
			case KeyCode.DownArrow:
				return 'Down';
			case KeyCode.LeftArrow:
				return 'Left';
			case KeyCode.RightArrow:
				return 'Right';
		}

		return KeyCode.toString(keyCode);
	}
}

/**
 * Print for Mac UI
 */
606
export class MacUIKeyLabelProvider implements IKeyBindingLabelProvider {
E
Erich Gamma 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
	public static INSTANCE = new MacUIKeyLabelProvider();

	private static leftArrowUnicodeLabel = String.fromCharCode(8592);
	private static upArrowUnicodeLabel = String.fromCharCode(8593);
	private static rightArrowUnicodeLabel = String.fromCharCode(8594);
	private static downArrowUnicodeLabel = String.fromCharCode(8595);

	public ctrlKeyLabel = '\u2303';
	public shiftKeyLabel = '\u21E7';
	public altKeyLabel = '\u2325';
	public cmdKeyLabel = '\u2318';
	public windowsKeyLabel = nls.localize('windowsKey', "Windows");
	public modifierSeparator = '';

	public getLabelForKey(keyCode:KeyCode): string {
		switch (keyCode) {
			case KeyCode.LeftArrow:
				return MacUIKeyLabelProvider.leftArrowUnicodeLabel;
			case KeyCode.UpArrow:
				return MacUIKeyLabelProvider.upArrowUnicodeLabel;
			case KeyCode.RightArrow:
				return MacUIKeyLabelProvider.rightArrowUnicodeLabel;
			case KeyCode.DownArrow:
				return MacUIKeyLabelProvider.downArrowUnicodeLabel;
		}

		return KeyCode.toString(keyCode);
	}
}

/**
 * Print for Windows, Linux UI
 */
640
export class ClassicUIKeyLabelProvider implements IKeyBindingLabelProvider {
E
Erich Gamma 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
	public static INSTANCE = new ClassicUIKeyLabelProvider();

	public ctrlKeyLabel = nls.localize('ctrlKey', "Ctrl");
	public shiftKeyLabel = nls.localize('shiftKey', "Shift");
	public altKeyLabel = nls.localize('altKey', "Alt");
	public cmdKeyLabel = nls.localize('cmdKey', "Command");
	public windowsKeyLabel = nls.localize('windowsKey', "Windows");
	public modifierSeparator = '+';

	public getLabelForKey(keyCode:KeyCode): string {
		return KeyCode.toString(keyCode);
	}
}

/**
 * Print for the user settings file.
 */
class UserSettingsKeyLabelProvider implements IKeyBindingLabelProvider {
	public static INSTANCE = new UserSettingsKeyLabelProvider();

	public ctrlKeyLabel = 'Ctrl';
	public shiftKeyLabel = 'Shift';
	public altKeyLabel = 'Alt';
	public cmdKeyLabel = 'Meta';
	public windowsKeyLabel = 'Meta';

	public modifierSeparator = '+';

	public getLabelForKey(keyCode:KeyCode): string {
		return KeyCode.toString(keyCode);
	}
}

A
Alex Dima 已提交
674
function _asString(keybinding:number, labelProvider:IKeyBindingLabelProvider, Platform:ISimplifiedPlatform): string {
E
Erich Gamma 已提交
675 676 677 678 679 680 681
	let result:string[] = [],
		ctrlCmd = BinaryKeybindings.hasCtrlCmd(keybinding),
		shift = BinaryKeybindings.hasShift(keybinding),
		alt = BinaryKeybindings.hasAlt(keybinding),
		winCtrl = BinaryKeybindings.hasWinCtrl(keybinding),
		keyCode = BinaryKeybindings.extractKeyCode(keybinding);

682 683 684 685 686 687
	let keyLabel = labelProvider.getLabelForKey(keyCode);
	if (!keyLabel) {
		// cannot trigger this key code under this kb layout
		return '';
	}

E
Erich Gamma 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
	// translate modifier keys: Ctrl-Shift-Alt-Meta
	if ((ctrlCmd && !Platform.isMacintosh) || (winCtrl && Platform.isMacintosh)) {
		result.push(labelProvider.ctrlKeyLabel);
	}

	if (shift) {
		result.push(labelProvider.shiftKeyLabel);
	}

	if (alt) {
		result.push(labelProvider.altKeyLabel);
	}

	if (ctrlCmd && Platform.isMacintosh) {
		result.push(labelProvider.cmdKeyLabel);
	}

	if (winCtrl && !Platform.isMacintosh) {
		result.push(labelProvider.windowsKeyLabel);
	}

	// the actual key
710
	result.push(keyLabel);
E
Erich Gamma 已提交
711 712 713 714

	var actualResult = result.join(labelProvider.modifierSeparator);

	if (BinaryKeybindings.hasChord(keybinding)) {
A
Alex Dima 已提交
715
		return actualResult + ' ' + _asString(BinaryKeybindings.extractChordPart(keybinding), labelProvider, Platform);
E
Erich Gamma 已提交
716 717 718 719
	}

	return actualResult;
}
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734

function _pushKey(result:IHTMLContentElement[], str:string): void {
	if (result.length > 0) {
		result.push({
			tagName: 'span',
			text: '+'
		});
	}
	result.push({
		tagName: 'span',
		className: 'monaco-kbkey',
		text: str
	});
}

A
Alex Dima 已提交
735
function _asHTML(keybinding:number, labelProvider:IKeyBindingLabelProvider, Platform:ISimplifiedPlatform, isChord:boolean = false): IHTMLContentElement[] {
736 737 738 739 740 741 742
	let result:IHTMLContentElement[] = [],
		ctrlCmd = BinaryKeybindings.hasCtrlCmd(keybinding),
		shift = BinaryKeybindings.hasShift(keybinding),
		alt = BinaryKeybindings.hasAlt(keybinding),
		winCtrl = BinaryKeybindings.hasWinCtrl(keybinding),
		keyCode = BinaryKeybindings.extractKeyCode(keybinding);

743 744 745 746 747 748
	let keyLabel = labelProvider.getLabelForKey(keyCode);
	if (!keyLabel) {
		// cannot trigger this key code under this kb layout
		return [];
	}

749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
	// translate modifier keys: Ctrl-Shift-Alt-Meta
	if ((ctrlCmd && !Platform.isMacintosh) || (winCtrl && Platform.isMacintosh)) {
		_pushKey(result, labelProvider.ctrlKeyLabel);
	}

	if (shift) {
		_pushKey(result, labelProvider.shiftKeyLabel);
	}

	if (alt) {
		_pushKey(result, labelProvider.altKeyLabel);
	}

	if (ctrlCmd && Platform.isMacintosh) {
		_pushKey(result, labelProvider.cmdKeyLabel);
	}

	if (winCtrl && !Platform.isMacintosh) {
		_pushKey(result, labelProvider.windowsKeyLabel);
	}

	// the actual key
771
	_pushKey(result, keyLabel);
772 773 774 775

	let chordTo: IHTMLContentElement[] = null;

	if (BinaryKeybindings.hasChord(keybinding)) {
A
Alex Dima 已提交
776
		chordTo = _asHTML(BinaryKeybindings.extractChordPart(keybinding), labelProvider, Platform, true);
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
		result.push({
			tagName: 'span',
			text: ' '
		});
		result = result.concat(chordTo);
	}

	if (isChord) {
		return result;
	}

	return [{
		tagName: 'span',
		className: 'monaco-kb',
		children: result
	}]

	return result;
}