-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.js
65 lines (54 loc) · 1.25 KB
/
keys.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
// For specifics of key code support see:
// http://www.quirksmode.org/js/keys.html
var keys = {};
keys.lookup = {
8: 'backspace'
, 9: 'tab'
, 13: 'enter'
, 27: 'esc'
, 32: 'space'
, 33: 'page-up'
, 34: 'page-down'
, 35: 'end'
, 36: 'home'
, 37: 'left'
, 39: 'right'
, 38: 'up'
, 40: 'down'
, 46: 'delete'
};
keys.name = function(e){
var code = e.keyCode;
var isAtoZ = code >= 65 && code <= 90;
var is0to9 = code >= 48 && code <= 57;
var isMeta = code == 91 || code == 93 || (code >= 16 && code <= 18);
var char, meta = [];
if (e.metaKey) meta.push('cmd');
if (e.ctrlKey) meta.push('ctrl');
if (e.altKey) meta.push('alt');
if (e.shiftKey) {
if (isAtoZ) {
char = String.fromCharCode(code);
} else {
meta.push('shift');
}
} else if (isAtoZ) {
char = String.fromCharCode(code + 32);
} else if (is0to9) {
char = '' + (code - 48);
}
if (!char && !isMeta) {
char = this.lookup[code] || '[' + code + ']';
}
if (meta.length) {
if (char) meta.push(char);
char = meta.join('-');
}
return char;
};
keys.addHandler = function(handler){
$(document).keydown(function(e){
var caught = handler(keys.name(e));
if (caught) e.stopImmediatePropagation();
});
};