UNPKG

6.94 kBJavaScriptView Raw
1/*
2 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16export var KeyCodes = {
17 8: "backspace",
18 9: "tab",
19 13: "enter",
20 20: "capslock",
21 27: "esc",
22 32: "space",
23 33: "pageup",
24 34: "pagedown",
25 35: "end",
26 36: "home",
27 37: "left",
28 38: "up",
29 39: "right",
30 40: "down",
31 45: "ins",
32 46: "del",
33 // number keys
34 48: "0",
35 49: "1",
36 50: "2",
37 51: "3",
38 52: "4",
39 53: "5",
40 54: "6",
41 55: "7",
42 56: "8",
43 57: "9",
44 // alphabet
45 65: "a",
46 66: "b",
47 67: "c",
48 68: "d",
49 69: "e",
50 70: "f",
51 71: "g",
52 72: "h",
53 73: "i",
54 74: "j",
55 75: "k",
56 76: "l",
57 77: "m",
58 78: "n",
59 79: "o",
60 80: "p",
61 81: "q",
62 82: "r",
63 83: "s",
64 84: "t",
65 85: "u",
66 86: "v",
67 87: "w",
68 88: "x",
69 89: "y",
70 90: "z",
71 // punctuation
72 106: "*",
73 107: "+",
74 109: "-",
75 110: ".",
76 111: "/",
77 186: ";",
78 187: "=",
79 188: ",",
80 189: "-",
81 190: ".",
82 191: "/",
83 192: "`",
84 219: "[",
85 220: "\\",
86 221: "]",
87 222: "'",
88};
89export var Modifiers = {
90 16: "shift",
91 17: "ctrl",
92 18: "alt",
93 91: "meta",
94 93: "meta",
95 224: "meta",
96};
97export var ModifierBitMasks = {
98 alt: 1,
99 ctrl: 2,
100 meta: 4,
101 shift: 8,
102};
103export var Aliases = {
104 cmd: "meta",
105 command: "meta",
106 escape: "esc",
107 minus: "-",
108 mod: isMac() ? "meta" : "ctrl",
109 option: "alt",
110 plus: "+",
111 return: "enter",
112 win: "meta",
113};
114// alph sorting is unintuitive here
115// tslint:disable object-literal-sort-keys
116export var ShiftKeys = {
117 "~": "`",
118 "!": "1",
119 "@": "2",
120 "#": "3",
121 $: "4",
122 "%": "5",
123 "^": "6",
124 "&": "7",
125 "*": "8",
126 "(": "9",
127 ")": "0",
128 _: "-",
129 "+": "=",
130 "{": "[",
131 "}": "]",
132 "|": "\\",
133 ":": ";",
134 '"': "'",
135 "<": ",",
136 ">": ".",
137 "?": "/",
138};
139// tslint:enable object-literal-sort-keys
140// Function keys
141for (var i = 1; i <= 12; ++i) {
142 KeyCodes[111 + i] = "f" + i;
143}
144// Numpad
145for (var i = 0; i <= 9; ++i) {
146 KeyCodes[96 + i] = "num" + i.toString();
147}
148export function comboMatches(a, b) {
149 return a.modifiers === b.modifiers && a.key === b.key;
150}
151/**
152 * Converts a key combo string into a key combo object. Key combos include
153 * zero or more modifier keys, such as `shift` or `alt`, and exactly one
154 * action key, such as `A`, `enter`, or `left`.
155 *
156 * For action keys that require a shift, e.g. `@` or `|`, we inlude the
157 * necessary `shift` modifier and automatically convert the action key to the
158 * unshifted version. For example, `@` is equivalent to `shift+2`.
159 */
160export var parseKeyCombo = function (combo) {
161 var pieces = combo.replace(/\s/g, "").toLowerCase().split("+");
162 var modifiers = 0;
163 var key;
164 for (var _i = 0, pieces_1 = pieces; _i < pieces_1.length; _i++) {
165 var piece = pieces_1[_i];
166 if (piece === "") {
167 throw new Error("Failed to parse key combo \"" + combo + "\".\n Valid key combos look like \"cmd + plus\", \"shift+p\", or \"!\"");
168 }
169 if (Aliases[piece] != null) {
170 piece = Aliases[piece];
171 }
172 if (ModifierBitMasks[piece] != null) {
173 modifiers += ModifierBitMasks[piece];
174 }
175 else if (ShiftKeys[piece] != null) {
176 modifiers += ModifierBitMasks.shift;
177 key = ShiftKeys[piece];
178 }
179 else {
180 key = piece.toLowerCase();
181 }
182 }
183 return { modifiers: modifiers, key: key };
184};
185/**
186 * Converts a keyboard event into a valid combo prop string
187 */
188export var getKeyComboString = function (e) {
189 var keys = [];
190 // modifiers first
191 if (e.ctrlKey) {
192 keys.push("ctrl");
193 }
194 if (e.altKey) {
195 keys.push("alt");
196 }
197 if (e.shiftKey) {
198 keys.push("shift");
199 }
200 if (e.metaKey) {
201 keys.push("meta");
202 }
203 // HACKHACK: https://github.com/palantir/blueprint/issues/4165
204 // eslint-disable-next-line deprecation/deprecation
205 var which = e.which;
206 if (Modifiers[which] != null) {
207 // no action key
208 }
209 else if (KeyCodes[which] != null) {
210 keys.push(KeyCodes[which]);
211 }
212 else {
213 // eslint-disable-next-line id-blacklist
214 keys.push(String.fromCharCode(which).toLowerCase());
215 }
216 // join keys with plusses
217 return keys.join(" + ");
218};
219/**
220 * Determines the key combo object from the given keyboard event. Again, a key
221 * combo includes zero or more modifiers (represented by a bitmask) and one
222 * action key, which we determine from the `e.which` property of the keyboard
223 * event.
224 */
225export var getKeyCombo = function (e) {
226 var key;
227 // HACKHACK: https://github.com/palantir/blueprint/issues/4165
228 // eslint-disable-next-line deprecation/deprecation
229 var which = e.which;
230 if (Modifiers[which] != null) {
231 // keep key null
232 }
233 else if (KeyCodes[which] != null) {
234 key = KeyCodes[which];
235 }
236 else {
237 // eslint-disable-next-line id-blacklist
238 key = String.fromCharCode(which).toLowerCase();
239 }
240 var modifiers = 0;
241 if (e.altKey) {
242 modifiers += ModifierBitMasks.alt;
243 }
244 if (e.ctrlKey) {
245 modifiers += ModifierBitMasks.ctrl;
246 }
247 if (e.metaKey) {
248 modifiers += ModifierBitMasks.meta;
249 }
250 if (e.shiftKey) {
251 modifiers += ModifierBitMasks.shift;
252 }
253 return { modifiers: modifiers, key: key };
254};
255/**
256 * Splits a key combo string into its constituent key values and looks up
257 * aliases, such as `return` -> `enter`.
258 *
259 * Unlike the parseKeyCombo method, this method does NOT convert shifted
260 * action keys. So `"@"` will NOT be converted to `["shift", "2"]`).
261 */
262export var normalizeKeyCombo = function (combo, platformOverride) {
263 var keys = combo.replace(/\s/g, "").split("+");
264 return keys.map(function (key) {
265 var keyName = Aliases[key] != null ? Aliases[key] : key;
266 return keyName === "meta" ? (isMac(platformOverride) ? "cmd" : "ctrl") : keyName;
267 });
268};
269function isMac(platformOverride) {
270 var platform = platformOverride != null ? platformOverride : typeof navigator !== "undefined" ? navigator.platform : undefined;
271 return platform == null ? false : /Mac|iPod|iPhone|iPad/.test(platform);
272}
273//# sourceMappingURL=hotkeyParser.js.map
\No newline at end of file