| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
/*----------------------------------------------------------------------------- |
| 4 |
| Copyright (c) 2014-2017, PhosphorJS Contributors |
| 5 |
| |
| 6 |
| Distributed under the terms of the BSD 3-Clause License. |
| 7 |
| |
| 8 |
| The full license is in the file LICENSE, distributed with this software. |
| 9 |
|----------------------------------------------------------------------------*/ |
| 10 |
/** |
| 11 |
* @packageDocumentation |
| 12 |
* @module keyboard |
| 13 |
*/ |
| 14 |
|
| 15 |
/** |
| 16 |
* An object which represents an abstract keyboard layout. |
| 17 |
*/ |
| 18 |
export interface IKeyboardLayout { |
| 19 |
/** |
| 20 |
* The human readable name of the layout. |
| 21 |
* |
| 22 |
* This value is used primarily for display and debugging purposes. |
| 23 |
*/ |
| 24 |
readonly name: string; |
| 25 |
|
| 26 |
/** |
| 27 |
* Get an array of all key values supported by the layout. |
| 28 |
* |
| 29 |
* @returns A new array of the supported key values. |
| 30 |
* |
| 31 |
* #### Notes |
| 32 |
* This can be useful for authoring tools and debugging, when it's |
| 33 |
* necessary to know which keys are available for shortcut use. |
| 34 |
*/ |
| 35 |
keys(): string[]; |
| 36 |
|
| 37 |
/** |
| 38 |
* Test whether the given key is a valid value for the layout. |
| 39 |
* |
| 40 |
* @param key - The user provided key to test for validity. |
| 41 |
* |
| 42 |
* @returns `true` if the key is valid, `false` otherwise. |
| 43 |
*/ |
| 44 |
isValidKey(key: string): boolean; |
| 45 |
|
| 46 |
/** |
| 47 |
* Test whether the given key is a modifier key. |
| 48 |
* |
| 49 |
* @param key - The user provided key. |
| 50 |
* |
| 51 |
* @returns `true` if the key is a modifier key, `false` otherwise. |
| 52 |
* |
| 53 |
* #### Notes |
| 54 |
* This is necessary so that we don't process modifier keys pressed |
| 55 |
* in the middle of the key sequence. |
| 56 |
* E.g. "Shift C Ctrl P" is actually 4 keydown events: |
| 57 |
* "Shift", "Shift P", "Ctrl", "Ctrl P", |
| 58 |
* and events for "Shift" and "Ctrl" should be ignored. |
| 59 |
*/ |
| 60 |
isModifierKey(key: string): boolean; |
| 61 |
|
| 62 |
/** |
| 63 |
* Get the key for a `'keydown'` event. |
| 64 |
* |
| 65 |
* @param event - The event object for a `'keydown'` event. |
| 66 |
* |
| 67 |
* @returns The associated key value, or an empty string if the event |
| 68 |
* does not represent a valid primary key. |
| 69 |
*/ |
| 70 |
keyForKeydownEvent(event: KeyboardEvent): string; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get the global application keyboard layout instance. |
| 75 |
* |
| 76 |
* @returns The keyboard layout for use by the application. |
| 77 |
* |
| 78 |
* #### Notes |
| 79 |
* The default keyboard layout is US-English. |
| 80 |
*/ |
| 81 |
export function getKeyboardLayout(): IKeyboardLayout { |
| 82 |
return Private.keyboardLayout; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Set the global application keyboard layout instance. |
| 87 |
* |
| 88 |
* @param layout The keyboard layout for use by the application. |
| 89 |
* |
| 90 |
* #### Notes |
| 91 |
* The keyboard layout should typically be set on application startup |
| 92 |
* to a layout which is appropriate for the user's system. |
| 93 |
*/ |
| 94 |
export function setKeyboardLayout(layout: IKeyboardLayout): void { |
| 95 |
Private.keyboardLayout = layout; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* A concrete implementation of {@link IKeyboardLayout} based on keycodes. |
| 100 |
* |
| 101 |
* The `keyCode` property of a `'keydown'` event is a browser and OS |
| 102 |
* specific representation of the physical key (not character) which |
| 103 |
* was pressed on a keyboard. While not the most convenient API, it |
| 104 |
* is currently the only one which works reliably on all browsers. |
| 105 |
* |
| 106 |
* This class accepts a user-defined mapping of keycode to key, which |
| 107 |
* allows for reliable shortcuts tailored to the user's system. |
| 108 |
*/ |
| 109 |
export class KeycodeLayout implements IKeyboardLayout { |
| 110 |
/** |
| 111 |
* Construct a new keycode layout. |
| 112 |
* |
| 113 |
* @param name - The human readable name for the layout. |
| 114 |
* |
| 115 |
* @param codes - A mapping of keycode to key value. |
| 116 |
* |
| 117 |
* @param modifierKeys - Array of modifier key names |
| 118 |
*/ |
| 119 |
constructor( |
| 120 |
name: string, |
| 121 |
codes: KeycodeLayout.CodeMap, |
| 122 |
modifierKeys: string[] = [] |
| 123 |
) { |
| 124 |
this.name = name; |
| 125 |
this._codes = codes; |
| 126 |
this._keys = KeycodeLayout.extractKeys(codes); |
| 127 |
this._modifierKeys = KeycodeLayout.convertToKeySet(modifierKeys); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* The human readable name of the layout. |
| 132 |
*/ |
| 133 |
readonly name: string; |
| 134 |
|
| 135 |
/** |
| 136 |
* Get an array of the key values supported by the layout. |
| 137 |
* |
| 138 |
* @returns A new array of the supported key values. |
| 139 |
*/ |
| 140 |
keys(): string[] { |
| 141 |
return Object.keys(this._keys); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Test whether the given key is a valid value for the layout. |
| 146 |
* |
| 147 |
* @param key - The user provided key to test for validity. |
| 148 |
* |
| 149 |
* @returns `true` if the key is valid, `false` otherwise. |
| 150 |
*/ |
| 151 |
isValidKey(key: string): boolean { |
| 152 |
return key in this._keys; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Test whether the given key is a modifier key. |
| 157 |
* |
| 158 |
* @param key - The user provided key. |
| 159 |
* |
| 160 |
* @returns `true` if the key is a modifier key, `false` otherwise. |
| 161 |
*/ |
| 162 |
isModifierKey(key: string): boolean { |
| 163 |
return key in this._modifierKeys; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get the key for a `'keydown'` event. |
| 168 |
* |
| 169 |
* @param event - The event object for a `'keydown'` event. |
| 170 |
* |
| 171 |
* @returns The associated key value, or an empty string if |
| 172 |
* the event does not represent a valid primary key. |
| 173 |
*/ |
| 174 |
keyForKeydownEvent(event: KeyboardEvent): string { |
| 175 |
return this._codes[event.keyCode] || ''; |
| 176 |
} |
| 177 |
|
| 178 |
private _keys: KeycodeLayout.KeySet; |
| 179 |
private _codes: KeycodeLayout.CodeMap; |
| 180 |
private _modifierKeys: KeycodeLayout.KeySet; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* The namespace for the `KeycodeLayout` class statics. |
| 185 |
*/ |
| 186 |
export namespace KeycodeLayout { |
| 187 |
/** |
| 188 |
* A type alias for a keycode map. |
| 189 |
*/ |
| 190 |
export type CodeMap = { readonly [code: number]: string }; |
| 191 |
|
| 192 |
/** |
| 193 |
* A type alias for a key set. |
| 194 |
*/ |
| 195 |
export type KeySet = { readonly [key: string]: boolean }; |
| 196 |
|
| 197 |
/** |
| 198 |
* Extract the set of keys from a code map. |
| 199 |
* |
| 200 |
* @param codes - The code map of interest. |
| 201 |
* |
| 202 |
* @returns A set of the keys in the code map. |
| 203 |
*/ |
| 204 |
export function extractKeys(codes: CodeMap): KeySet { |
| 205 |
let keys: any = Object.create(null); |
| 206 |
for (let c in codes) { |
| 207 |
keys[codes[c]] = true; |
| 208 |
} |
| 209 |
return keys as KeySet; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Convert array of keys to a key set. |
| 214 |
* |
| 215 |
* @param keys - The array that needs to be converted |
| 216 |
* |
| 217 |
* @returns A set of the keys in the array. |
| 218 |
*/ |
| 219 |
export function convertToKeySet(keys: string[]): KeySet { |
| 220 |
let keySet = Object(null); |
| 221 |
for (let i = 0, n = keys.length; i < n; ++i) { |
| 222 |
keySet[keys[i]] = true; |
| 223 |
} |
| 224 |
return keySet; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* A keycode-based keyboard layout for US English keyboards. |
| 230 |
* |
| 231 |
* This layout is valid for the following OS/Browser combinations. |
| 232 |
* |
| 233 |
* - Windows |
| 234 |
* - Chrome |
| 235 |
* - Firefox |
| 236 |
* - IE |
| 237 |
* |
| 238 |
* - OSX |
| 239 |
* - Chrome |
| 240 |
* - Firefox |
| 241 |
* - Safari |
| 242 |
* |
| 243 |
* - Linux |
| 244 |
* - Chrome |
| 245 |
* - Firefox |
| 246 |
* |
| 247 |
* Other combinations may also work, but are untested. |
| 248 |
*/ |
| 249 |
export const EN_US: IKeyboardLayout = new KeycodeLayout( |
| 250 |
'en-us', |
| 251 |
{ |
| 252 |
8: 'Backspace', |
| 253 |
9: 'Tab', |
| 254 |
13: 'Enter', |
| 255 |
16: 'Shift', |
| 256 |
17: 'Ctrl', |
| 257 |
18: 'Alt', |
| 258 |
19: 'Pause', |
| 259 |
27: 'Escape', |
| 260 |
32: 'Space', |
| 261 |
33: 'PageUp', |
| 262 |
34: 'PageDown', |
| 263 |
35: 'End', |
| 264 |
36: 'Home', |
| 265 |
37: 'ArrowLeft', |
| 266 |
38: 'ArrowUp', |
| 267 |
39: 'ArrowRight', |
| 268 |
40: 'ArrowDown', |
| 269 |
45: 'Insert', |
| 270 |
46: 'Delete', |
| 271 |
48: '0', |
| 272 |
49: '1', |
| 273 |
50: '2', |
| 274 |
51: '3', |
| 275 |
52: '4', |
| 276 |
53: '5', |
| 277 |
54: '6', |
| 278 |
55: '7', |
| 279 |
56: '8', |
| 280 |
57: '9', |
| 281 |
59: ';', // firefox |
| 282 |
61: '=', // firefox |
| 283 |
65: 'A', |
| 284 |
66: 'B', |
| 285 |
67: 'C', |
| 286 |
68: 'D', |
| 287 |
69: 'E', |
| 288 |
70: 'F', |
| 289 |
71: 'G', |
| 290 |
72: 'H', |
| 291 |
73: 'I', |
| 292 |
74: 'J', |
| 293 |
75: 'K', |
| 294 |
76: 'L', |
| 295 |
77: 'M', |
| 296 |
78: 'N', |
| 297 |
79: 'O', |
| 298 |
80: 'P', |
| 299 |
81: 'Q', |
| 300 |
82: 'R', |
| 301 |
83: 'S', |
| 302 |
84: 'T', |
| 303 |
85: 'U', |
| 304 |
86: 'V', |
| 305 |
87: 'W', |
| 306 |
88: 'X', |
| 307 |
89: 'Y', |
| 308 |
90: 'Z', |
| 309 |
91: 'Meta', // non-firefox |
| 310 |
93: 'ContextMenu', |
| 311 |
96: '0', // numpad |
| 312 |
97: '1', // numpad |
| 313 |
98: '2', // numpad |
| 314 |
99: '3', // numpad |
| 315 |
100: '4', // numpad |
| 316 |
101: '5', // numpad |
| 317 |
102: '6', // numpad |
| 318 |
103: '7', // numpad |
| 319 |
104: '8', // numpad |
| 320 |
105: '9', // numpad |
| 321 |
106: '*', // numpad |
| 322 |
107: '+', // numpad |
| 323 |
109: '-', // numpad |
| 324 |
110: '.', // numpad |
| 325 |
111: '/', // numpad |
| 326 |
112: 'F1', |
| 327 |
113: 'F2', |
| 328 |
114: 'F3', |
| 329 |
115: 'F4', |
| 330 |
116: 'F5', |
| 331 |
117: 'F6', |
| 332 |
118: 'F7', |
| 333 |
119: 'F8', |
| 334 |
120: 'F9', |
| 335 |
121: 'F10', |
| 336 |
122: 'F11', |
| 337 |
123: 'F12', |
| 338 |
173: '-', // firefox |
| 339 |
186: ';', // non-firefox |
| 340 |
187: '=', // non-firefox |
| 341 |
188: ',', |
| 342 |
189: '-', // non-firefox |
| 343 |
190: '.', |
| 344 |
191: '/', |
| 345 |
192: '`', |
| 346 |
219: '[', |
| 347 |
220: '\\', |
| 348 |
221: ']', |
| 349 |
222: "'", |
| 350 |
224: 'Meta' // firefox |
| 351 |
}, |
| 352 |
['Shift', 'Ctrl', 'Alt', 'Meta'] // modifier keys |
| 353 |
); |
| 354 |
|
| 355 |
/** |
| 356 |
* The namespace for the module implementation details. |
| 357 |
*/ |
| 358 |
namespace Private { |
| 359 |
/** |
| 360 |
* The global keyboard layout instance. |
| 361 |
*/ |
| 362 |
export let keyboardLayout = EN_US; |
| 363 |
} |