UNPKG

4.28 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2020 Google Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23/**
24 * KEY provides normalized string values for keys.
25 */
26export var KEY = {
27 UNKNOWN: 'Unknown',
28 BACKSPACE: 'Backspace',
29 ENTER: 'Enter',
30 SPACEBAR: 'Spacebar',
31 PAGE_UP: 'PageUp',
32 PAGE_DOWN: 'PageDown',
33 END: 'End',
34 HOME: 'Home',
35 ARROW_LEFT: 'ArrowLeft',
36 ARROW_UP: 'ArrowUp',
37 ARROW_RIGHT: 'ArrowRight',
38 ARROW_DOWN: 'ArrowDown',
39 DELETE: 'Delete',
40 ESCAPE: 'Escape',
41 TAB: 'Tab',
42};
43var normalizedKeys = new Set();
44// IE11 has no support for new Map with iterable so we need to initialize this
45// by hand.
46normalizedKeys.add(KEY.BACKSPACE);
47normalizedKeys.add(KEY.ENTER);
48normalizedKeys.add(KEY.SPACEBAR);
49normalizedKeys.add(KEY.PAGE_UP);
50normalizedKeys.add(KEY.PAGE_DOWN);
51normalizedKeys.add(KEY.END);
52normalizedKeys.add(KEY.HOME);
53normalizedKeys.add(KEY.ARROW_LEFT);
54normalizedKeys.add(KEY.ARROW_UP);
55normalizedKeys.add(KEY.ARROW_RIGHT);
56normalizedKeys.add(KEY.ARROW_DOWN);
57normalizedKeys.add(KEY.DELETE);
58normalizedKeys.add(KEY.ESCAPE);
59normalizedKeys.add(KEY.TAB);
60var KEY_CODE = {
61 BACKSPACE: 8,
62 ENTER: 13,
63 SPACEBAR: 32,
64 PAGE_UP: 33,
65 PAGE_DOWN: 34,
66 END: 35,
67 HOME: 36,
68 ARROW_LEFT: 37,
69 ARROW_UP: 38,
70 ARROW_RIGHT: 39,
71 ARROW_DOWN: 40,
72 DELETE: 46,
73 ESCAPE: 27,
74 TAB: 9,
75};
76var mappedKeyCodes = new Map();
77// IE11 has no support for new Map with iterable so we need to initialize this
78// by hand.
79mappedKeyCodes.set(KEY_CODE.BACKSPACE, KEY.BACKSPACE);
80mappedKeyCodes.set(KEY_CODE.ENTER, KEY.ENTER);
81mappedKeyCodes.set(KEY_CODE.SPACEBAR, KEY.SPACEBAR);
82mappedKeyCodes.set(KEY_CODE.PAGE_UP, KEY.PAGE_UP);
83mappedKeyCodes.set(KEY_CODE.PAGE_DOWN, KEY.PAGE_DOWN);
84mappedKeyCodes.set(KEY_CODE.END, KEY.END);
85mappedKeyCodes.set(KEY_CODE.HOME, KEY.HOME);
86mappedKeyCodes.set(KEY_CODE.ARROW_LEFT, KEY.ARROW_LEFT);
87mappedKeyCodes.set(KEY_CODE.ARROW_UP, KEY.ARROW_UP);
88mappedKeyCodes.set(KEY_CODE.ARROW_RIGHT, KEY.ARROW_RIGHT);
89mappedKeyCodes.set(KEY_CODE.ARROW_DOWN, KEY.ARROW_DOWN);
90mappedKeyCodes.set(KEY_CODE.DELETE, KEY.DELETE);
91mappedKeyCodes.set(KEY_CODE.ESCAPE, KEY.ESCAPE);
92mappedKeyCodes.set(KEY_CODE.TAB, KEY.TAB);
93var navigationKeys = new Set();
94// IE11 has no support for new Set with iterable so we need to initialize this
95// by hand.
96navigationKeys.add(KEY.PAGE_UP);
97navigationKeys.add(KEY.PAGE_DOWN);
98navigationKeys.add(KEY.END);
99navigationKeys.add(KEY.HOME);
100navigationKeys.add(KEY.ARROW_LEFT);
101navigationKeys.add(KEY.ARROW_UP);
102navigationKeys.add(KEY.ARROW_RIGHT);
103navigationKeys.add(KEY.ARROW_DOWN);
104/**
105 * normalizeKey returns the normalized string for a navigational action.
106 */
107export function normalizeKey(evt) {
108 var key = evt.key;
109 // If the event already has a normalized key, return it
110 if (normalizedKeys.has(key)) {
111 return key;
112 }
113 // tslint:disable-next-line:deprecation
114 var mappedKey = mappedKeyCodes.get(evt.keyCode);
115 if (mappedKey) {
116 return mappedKey;
117 }
118 return KEY.UNKNOWN;
119}
120/**
121 * isNavigationEvent returns whether the event is a navigation event
122 */
123export function isNavigationEvent(evt) {
124 return navigationKeys.has(normalizeKey(evt));
125}
126//# sourceMappingURL=keyboard.js.map
\No newline at end of file