-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.js
49 lines (43 loc) · 1.21 KB
/
input.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
function setupInput() {
if (!document.addEventListener && document.attachEvent) {
// IE
document.attachEvent('onkeydown', handleKeyDown)
document.attachEvent('onkeypress', handleKeyPress)
document.attachEvent('onkeyup', handleKeyUp)
} else {
window.addEventListener('keydown', handleKeyDown, true)
window.addEventListener('keypress', handleKeyPress, true)
window.addEventListener('keyup', handleKeyUp, true)
}
}
function handleKeyPress(event)
{
return stopPropagation(event.charCode, event);
}
function handleKeyDown(event)
{
if (event.keyCode == 32) // space
key_fire = true;
else if (event.keyCode == 37) // left
key_left = true;
else if (event.keyCode == 39) // right
key_right = true;
return stopPropagation(event.keyCode, event);
}
function handleKeyUp(event)
{
if (event.keyCode == 32) // space
key_fire = false;
else if (event.keyCode == 37) // left
key_left = false;
else if (event.keyCode == 39) // right
key_right = false;
return stopPropagation(event.keyCode, event);
}
function stopPropagation(keyCode, event) {
if (keyCode == 37 || keyCode == 39) {
if (event.preventDefault) event.preventDefault();
if (event.stopPropagation) event.stopPropagation();
return false;
}
}