forked from piranha/keymage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keymage.js
353 lines (297 loc) · 8.99 KB
/
keymage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
102
103
104
105
106
107
108
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
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
/** @license
* keymage.js - Javascript keyboard bindings handling
* http://github.com/piranha/keymage
*
* (c) 2012-2016 Alexander Solovyov under terms of ISC License
*/
(function(define, undefined) {
define(function() {
'use strict';
var VERSION = '1.1.3';
var isOsx = ~navigator.userAgent.indexOf('Mac OS X');
// Defining all keys
var MODPROPS = ['shiftKey', 'ctrlKey', 'altKey', 'metaKey'];
var MODS = {
'shift': 'shift',
'ctrl': 'ctrl', 'control': 'ctrl',
'alt': 'alt', 'option': 'alt',
'win': 'meta', 'cmd': 'meta', 'super': 'meta',
'meta': 'meta',
// default modifier for os x is cmd and for others is ctrl
'defmod': isOsx ? 'meta' : 'ctrl'
};
var MODORDER = ['shift', 'ctrl', 'alt', 'meta'];
var MODNUMS = [16, 17, 18, 91];
var KEYS = {
'backspace': 8,
'tab': 9,
'enter': 13, 'return': 13,
'pause': 19,
'caps': 20, 'capslock': 20,
'escape': 27, 'esc': 27,
'space': 32,
'pgup': 33, 'pageup': 33,
'pgdown': 34, 'pagedown': 34,
'end': 35,
'home': 36,
'ins': 45, 'insert': 45,
'del': 46, 'delete': 46,
'left': 37,
'up': 38,
'right': 39,
'down': 40,
'*': 106,
'+': 107, 'plus': 107,
'minus': 109,
';': 186,
'=': 187,
',': 188,
'-': 189,
'.': 190,
'/': 191,
'`': 192,
'[': 219,
'\\': 220,
']': 221,
"'": 222
};
var i;
// numpad
for (i = 0; i < 10; i++) {
KEYS['num-' + i] = i + 95;
}
// top row 0-9
for (i = 0; i < 10; i++) {
KEYS['' + i] = i + 48;
}
// f1-f24
for (i = 1; i < 25; i++) {
KEYS['f' + i] = i + 111;
}
// alphabet
for (i = 65; i < 91; i++) {
KEYS[String.fromCharCode(i).toLowerCase()] = i;
}
// Reverse key codes
var KEYREV = {};
Object.keys(KEYS).forEach(function(k) {
var val = KEYS[k];
if (!KEYREV[val] || KEYREV[val].length < k.length) {
KEYREV[val] = k;
}
});
// -----------------------
// Actual work is done here
var currentScope = '';
var allChains = {};
function parseKeyString(keystring) {
var bits = keystring.split(/-(?!$)/);
var button = bits[bits.length - 1];
var key = {code: KEYS[button]};
if (!key.code) {
throw 'Unknown key "' + button + '" in keystring "' +
keystring + '"';
}
var mod;
for (var i = 0; i < bits.length - 1; i++) {
button = bits[i];
mod = MODS[button];
if (!mod) {
throw 'Unknown modifier "' + button + '" in keystring "' +
keystring + '"';
}
key[mod] = true;
}
return key;
}
function stringifyKey(key) {
var s = '';
for (var i = 0; i < MODORDER.length; i++) {
if (key[MODORDER[i]]) {
s += MODORDER[i] + '-';
}
}
s += KEYREV[key.code];
return s;
}
function normalizeKeyChain(keychainString) {
var keychain = [];
var keys = keychainString.split(' ');
for (var i = 0; i < keys.length; i++) {
var key = parseKeyString(keys[i]);
key = stringifyKey(key);
keychain.push(key);
}
keychain.original = keychainString;
return keychain;
}
function eventKeyString(e) {
var key = {code: e.keyCode};
for (var i = 0; i < MODPROPS.length; i++) {
var mod = MODPROPS[i];
if (e[mod]) {
key[mod.slice(0, mod.length - 3)] = true;
}
}
return stringifyKey(key);
}
function getNestedChains(chains, scope) {
for (var i = 0; i < scope.length; i++) {
var bit = scope[i];
if (bit) {
chains = chains[bit];
}
if (!chains) {
break;
}
}
return chains;
}
var sequence = [];
function dispatch(e) {
// Skip all modifiers
if (~MODNUMS.indexOf(e.keyCode)) {
return;
}
var seq = sequence.slice();
seq.push(eventKeyString(e));
var scope = currentScope.split('.');
var matched, chains, key;
for (var i = scope.length; i >= 0; i--) {
chains = getNestedChains(allChains, scope.slice(0, i));
if (!chains) {
continue;
}
matched = true;
for (var j = 0; j < seq.length; j++) {
key = seq[j];
if (!chains[key]) {
matched = false;
break;
}
chains = chains[key];
}
if (matched) {
break;
}
}
var definitionScope = scope.slice(0, i).join('.');
var preventDefault = chains.preventDefault;
// partial match, save the sequence
if (matched && !chains.handlers) {
sequence = seq;
if (preventDefault) {
e.preventDefault();
}
return;
}
if (matched) {
for (i = 0; i < chains.handlers.length; i++) {
var handler = chains.handlers[i];
var options = handler._keymage;
var res = handler.call(options.context, e, {
shortcut: options.original,
scope: currentScope,
definitionScope: definitionScope
});
if (res === false || preventDefault) {
e.preventDefault();
}
}
}
// either matched or not, drop the sequence
sequence = [];
}
function getHandlers(scope, keychain, fn) {
var bits = scope.split('.');
var chains = allChains;
bits = bits.concat(keychain);
for (var i = 0, l = bits.length; i < l; i++) {
var bit = bits[i];
if (!bit) continue;
chains = chains[bit] || (chains[bit] = {});
if (fn && fn._keymage.preventDefault) {
chains.preventDefault = true;
}
if (i === l - 1) {
var handlers = chains.handlers || (chains.handlers = []);
return handlers;
}
}
}
function assignKey(scope, keychain, fn) {
var handlers = getHandlers(scope, keychain, fn);
handlers.push(fn);
}
function unassignKey(scope, keychain, fn) {
var handlers = getHandlers(scope, keychain);
var idx = handlers.indexOf(fn);
if (~idx) {
handlers.splice(idx, 1);
}
}
function parsed(scope, keychain, fn, options) {
if (keychain === undefined && fn === undefined) {
return function(keychain, fn) {
return keymage(scope, keychain, fn);
};
}
if (typeof keychain === 'function') {
options = fn;
fn = keychain;
keychain = scope;
scope = '';
}
var normalized = normalizeKeyChain(keychain);
return [scope, normalized, fn, options];
}
// optional arguments: scope, options.
function keymage(scope, keychain, fn, options) {
var args = parsed(scope, keychain, fn, options);
fn = args[2];
options = args[3];
fn._keymage = options || {};
fn._keymage.original = keychain;
assignKey.apply(null, args);
return function () {
unassignKey.apply(null, args);
};
}
keymage.unbind = function(scope, keychain, fn) {
var args = parsed(scope, keychain, fn);
unassignKey.apply(null, args);
};
keymage.parse = parseKeyString;
keymage.stringify = stringifyKey;
keymage.bindings = allChains;
keymage.setScope = function(scope) {
currentScope = scope ? scope : '';
};
keymage.getScope = function() { return currentScope; };
keymage.pushScope = function(scope) {
currentScope = (currentScope ? currentScope + '.' : '') + scope;
return currentScope;
};
keymage.popScope = function(scope) {
var i;
if (!scope) {
i = currentScope.lastIndexOf('.');
scope = currentScope.slice(i + 1);
currentScope = i == -1 ? '' : currentScope.slice(0, i);
return scope;
}
currentScope = currentScope.replace(
new RegExp('(^|\\.)' + scope + '(\\.|$).*'), '');
return scope;
};
keymage.version = VERSION;
window.addEventListener('keydown', dispatch);
return keymage;
});
})(typeof define !== 'undefined' ? define : function(factory) {
if (typeof module !== 'undefined') {
module.exports = factory();
} else {
window.keymage = factory();
}
});