-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.js
86 lines (74 loc) · 1.36 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const wKey = keyboard(87),
aKey = keyboard(65),
sKey = keyboard(83),
dKey = keyboard(68);
let keyx = 0,
keyy = 0;
const fKey = keyboard(70);
let fire = false;
function input() {
aKey.press = function() {
--keyx;
};
aKey.release = function() {
++keyx;
};
wKey.press = function() {
--keyy;
};
wKey.release = function() {
++keyy;
};
dKey.press = function() {
++keyx;
};
dKey.release = function() {
--keyx;
};
sKey.press = function() {
++keyy;
};
sKey.release = function() {
--keyy;
};
fKey.press = function(){
fire = true;
}
fKey.release = function(){
fire = false;
}
}
function keyboard(keyCode) {
var key = {};
key.code = keyCode;
key.isDown = false;
key.isUp = true;
key.press = undefined;
key.release = undefined;
//The `downHandler`
key.downHandler = function(event) {
if (event.keyCode === key.code) {
if (key.isUp && key.press) key.press();
key.isDown = true;
key.isUp = false;
}
event.preventDefault();
};
//The `upHandler`
key.upHandler = function(event) {
if (event.keyCode === key.code) {
if (key.isDown && key.release) key.release();
key.isDown = false;
key.isUp = true;
}
event.preventDefault();
};
//Attach event listeners
window.addEventListener(
"keydown", key.downHandler.bind(key), false
);
window.addEventListener(
"keyup", key.upHandler.bind(key), false
);
return key;
}