-
Notifications
You must be signed in to change notification settings - Fork 2
/
isHoldingKey.js
52 lines (47 loc) · 993 Bytes
/
isHoldingKey.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
// Quick little library for telling whether a key is being held down.
;(function(global){
// Inverse of index in keymaster.js.
var _MAP = {
8: 'backspace',
9: 'tab',
12: 'clear',
13: 'return',
27: 'escape',
32: 'space',
37: 'left',
38: 'up',
39: 'right',
40: 'down',
46: 'delete',
36: 'home',
35: 'end',
33: 'pageup',
34: 'pagedown',
188: ',',
190: '.',
191: '/',
192: '`',
189: '-',
187: '=',
186: ';',
222: '\'',
219: '[',
221: ']',
220: '\\'
};
var heldKeys = {};
function handler(event) {
var keyName = _MAP[event.keyCode] ||
String.fromCharCode(event.keyCode).toLowerCase();
if (event.type == 'keydown') {
heldKeys[keyName] = true;
} else {
delete heldKeys[keyName];
};
};
global.addEventListener('keydown', handler, false);
global.addEventListener('keyup', handler, false);
global.isHoldingKey = function(key) {
return key === undefined ? heldKeys : heldKeys[key];
};
})(this);