forked from madrobby/keymaster
-
Notifications
You must be signed in to change notification settings - Fork 2
/
keymaster.js
69 lines (63 loc) · 2.26 KB
/
keymaster.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
// keymaster.js
// (c) 2011 Thomas Fuchs
// keymaster.js may be freely distributed under the MIT license.
;(function(global){
var _handlers = {},
_mods = { 16: false, 18: false, 17: false, 91: false },
_scope = 'all',
_MODIFIERS = {
shift: 16, option: 18, '⌥': 18, alt: 18, ctrl: 17, control: 17, command: 91, '⌘': 91 },
_MAP = {
backspace: 8, tab: 9,
enter: 13, 'return': 13,
escape: 27, space: 32,
left: 37, up: 38,
right: 39, down: 40 };
function dispatch(event){
var key, tagName;
tagName = event.target.tagName;
key = event.keyCode;
if(key in _mods) return _mods[key] = true;
if (tagName == 'INPUT' || tagName == 'SELECT' || tagName == 'TEXTAREA') return;
if (!(key in _handlers)) return;
_handlers[key].forEach(function(handler){
if(handler.scope == _scope || handler.scope == 'all')
if((handler.mods.length == 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91]) ||
(handler.mods.length > 0 && handler.mods.every(function(mod){ return _mods[mod] })))
if(handler.method(event, handler.key, handler.scope)===false){
event.stopPropagation();
event.preventDefault();
}
});
}
function clearModifier(event){
var key = event.keyCode;
if(key in _mods) _mods[key] = false;
}
function assignKey(key, scope, method){
var keys, mods;
if (method === undefined) {
method = scope;
scope = 'all';
}
key = key.replace(/\s/g,'');
keys = key.split(',');
keys.forEach(function(originalKey){
mods = [];
key = originalKey.split('+');
if(key.length > 1){
mods = key.slice(0,key.length-1).map(function(mod){ return _MODIFIERS[mod] });
key = [key[key.length-1]];
}
key = key[0]
key = key.length > 1 ? _MAP[key] : key.toUpperCase().charCodeAt(0);
if (!(key in _handlers)) _handlers[key] = [];
_handlers[key].push({ scope: scope, method: method, key: originalKey, mods: mods });
});
}
function setScope(scope){ _scope = scope || 'all' }
document.addEventListener('keydown', dispatch, false);
document.addEventListener('keyup', clearModifier, false);
global.key = assignKey;
global.keyScope = setScope;
})(this);