-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifiers.js
50 lines (39 loc) · 998 Bytes
/
modifiers.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
(function() {
var modifiers = {
shift: 16,
control: 17,
alt: 18,
option: 18,
command: 91
};
var pressedModifiers = {};
function resetModifiers() {
for (var modifier in modifiers) {
pressedModifiers[modifier] = false;
}
};
function updateModifier(e) {
var key = e.keyCode;
var val = e.type == 'keydown';
if (key == 93 || key == 224) {
key = 91;
}
for (var modifier in modifiers) {
if (key == modifiers[modifier]) {
pressedModifiers[modifier] = val;
}
}
};
resetModifiers();
document.addEventListener('keydown', updateModifier);
document.addEventListener('keyup', updateModifier);
window.addEventListener('focus', resetModifiers);
this.modifiers = pressedModifiers;
// allow adding of additional keyCodes
this.modifiers.add = function addKeyMapping(map) {
for (var key in map) {
modifiers[key] = map[key];
pressedModifiers[key] = false;
}
}
}).call(this);