UNPKG

5.63 kBJavaScriptView Raw
1"use strict";
2/*-----------------------------------------------------------------------------
3| Copyright (c) 2014-2017, PhosphorJS Contributors
4|
5| Distributed under the terms of the BSD 3-Clause License.
6|
7| The full license is in the file LICENSE, distributed with this software.
8|----------------------------------------------------------------------------*/
9Object.defineProperty(exports, "__esModule", { value: true });
10/**
11 * Get the global application keyboard layout instance.
12 *
13 * @returns The keyboard layout for use by the application.
14 *
15 * #### Notes
16 * The default keyboard layout is US-English.
17 */
18function getKeyboardLayout() {
19 return Private.keyboardLayout;
20}
21exports.getKeyboardLayout = getKeyboardLayout;
22/**
23 * Set the global application keyboard layout instance.
24 *
25 * @param - The keyboard layout for use by the application.
26 *
27 * #### Notes
28 * The keyboard layout should typically be set on application startup
29 * to a layout which is appropriate for the user's system.
30 */
31function setKeyboardLayout(layout) {
32 Private.keyboardLayout = layout;
33}
34exports.setKeyboardLayout = setKeyboardLayout;
35/**
36 * A concrete implementation of [[IKeyboardLayout]] based on keycodes.
37 *
38 * The `keyCode` property of a `'keydown'` event is a browser and OS
39 * specific representation of the physical key (not character) which
40 * was pressed on a keyboard. While not the most convenient API, it
41 * is currently the only one which works reliably on all browsers.
42 *
43 * This class accepts a user-defined mapping of keycode to key, which
44 * allows for reliable shortcuts tailored to the user's system.
45 */
46var KeycodeLayout = /** @class */ (function () {
47 /**
48 * Construct a new keycode layout.
49 *
50 * @param name - The human readable name for the layout.
51 *
52 * @param codes - A mapping of keycode to key value.
53 */
54 function KeycodeLayout(name, codes) {
55 this.name = name;
56 this._codes = codes;
57 this._keys = KeycodeLayout.extractKeys(codes);
58 }
59 /**
60 * Get an array of the key values supported by the layout.
61 *
62 * @returns A new array of the supported key values.
63 */
64 KeycodeLayout.prototype.keys = function () {
65 return Object.keys(this._keys);
66 };
67 /**
68 * Test whether the given key is a valid value for the layout.
69 *
70 * @param key - The user provided key to test for validity.
71 *
72 * @returns `true` if the key is valid, `false` otherwise.
73 */
74 KeycodeLayout.prototype.isValidKey = function (key) {
75 return key in this._keys;
76 };
77 /**
78 * Get the key for a `'keydown'` event.
79 *
80 * @param event - The event object for a `'keydown'` event.
81 *
82 * @returns The associated key value, or an empty string if
83 * the event does not represent a valid primary key.
84 */
85 KeycodeLayout.prototype.keyForKeydownEvent = function (event) {
86 return this._codes[event.keyCode] || '';
87 };
88 return KeycodeLayout;
89}());
90exports.KeycodeLayout = KeycodeLayout;
91/**
92 * The namespace for the `KeycodeLayout` class statics.
93 */
94(function (KeycodeLayout) {
95 /**
96 * Extract the set of keys from a code map.
97 *
98 * @param code - The code map of interest.
99 *
100 * @returns A set of the keys in the code map.
101 */
102 function extractKeys(codes) {
103 var keys = Object.create(null);
104 for (var c in codes) {
105 keys[codes[c]] = true;
106 }
107 return keys;
108 }
109 KeycodeLayout.extractKeys = extractKeys;
110})(KeycodeLayout = exports.KeycodeLayout || (exports.KeycodeLayout = {}));
111exports.KeycodeLayout = KeycodeLayout;
112/**
113 * A keycode-based keyboard layout for US English keyboards.
114 *
115 * This layout is valid for the following OS/Browser combinations.
116 *
117 * - Windows
118 * - Chrome
119 * - Firefox
120 * - IE
121 *
122 * - OSX
123 * - Chrome
124 * - Firefox
125 * - Safari
126 *
127 * - Linux
128 * - Chrome
129 * - Firefox
130 *
131 * Other combinations may also work, but are untested.
132 */
133exports.EN_US = new KeycodeLayout('en-us', {
134 8: 'Backspace',
135 9: 'Tab',
136 13: 'Enter',
137 19: 'Pause',
138 27: 'Escape',
139 32: 'Space',
140 33: 'PageUp',
141 34: 'PageDown',
142 35: 'End',
143 36: 'Home',
144 37: 'ArrowLeft',
145 38: 'ArrowUp',
146 39: 'ArrowRight',
147 40: 'ArrowDown',
148 45: 'Insert',
149 46: 'Delete',
150 48: '0',
151 49: '1',
152 50: '2',
153 51: '3',
154 52: '4',
155 53: '5',
156 54: '6',
157 55: '7',
158 56: '8',
159 57: '9',
160 59: ';',
161 61: '=',
162 65: 'A',
163 66: 'B',
164 67: 'C',
165 68: 'D',
166 69: 'E',
167 70: 'F',
168 71: 'G',
169 72: 'H',
170 73: 'I',
171 74: 'J',
172 75: 'K',
173 76: 'L',
174 77: 'M',
175 78: 'N',
176 79: 'O',
177 80: 'P',
178 81: 'Q',
179 82: 'R',
180 83: 'S',
181 84: 'T',
182 85: 'U',
183 86: 'V',
184 87: 'W',
185 88: 'X',
186 89: 'Y',
187 90: 'Z',
188 93: 'ContextMenu',
189 96: '0',
190 97: '1',
191 98: '2',
192 99: '3',
193 100: '4',
194 101: '5',
195 102: '6',
196 103: '7',
197 104: '8',
198 105: '9',
199 106: '*',
200 107: '+',
201 109: '-',
202 110: '.',
203 111: '/',
204 112: 'F1',
205 113: 'F2',
206 114: 'F3',
207 115: 'F4',
208 116: 'F5',
209 117: 'F6',
210 118: 'F7',
211 119: 'F8',
212 120: 'F9',
213 121: 'F10',
214 122: 'F11',
215 123: 'F12',
216 173: '-',
217 186: ';',
218 187: '=',
219 188: ',',
220 189: '-',
221 190: '.',
222 191: '/',
223 192: '`',
224 219: '[',
225 220: '\\',
226 221: ']',
227 222: '\''
228});
229/**
230 * The namespace for the module implementation details.
231 */
232var Private;
233(function (Private) {
234 /**
235 * The global keyboard layout instance.
236 */
237 Private.keyboardLayout = exports.EN_US;
238})(Private || (Private = {}));