-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.js
30 lines (30 loc) · 1.44 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
export class InputHandler {
constructor(){
this.keys = [];
console.log("hello");
window.addEventListener('keydown', e => {
// console.log(e.key, this.keys);
if (
(e.key === "Right" || e.key === "ArrowRight" || e.key === "d" || e.key === "D" ||
e.key === "Left" || e.key === "ArrowLeft" || e.key === "a" || e.key === "A" ||
e.key === "Up" || e.key === "ArrowUp" || e.key === "w" || e.key === "W" ||
e.key === "Down" || e.key === "ArrowDown" || e.key === "s" || e.key === "S" ||
e.key === " " || e.code === "Space" )
&& this.keys.indexOf(e.key) === -1) { //checks if key is already in array
this.keys.push(e.key);
}
// console.log(e.key, this.keys);
});
window.addEventListener('keyup', e => {
// console.log(e.key, this.keys);
if (
(e.key === "Right" || e.key === "ArrowRight" || e.key === "d" || e.key === "D") ||
(e.key === "Left" || e.key === "ArrowLeft" || e.key === "a" || e.key === "A") ||
(e.key === "Up" || e.key === "ArrowUp" || e.key === "w" || e.key === "W") ||
(e.key === "Down" || e.key === "ArrowDown" || e.key === "s" || e.key === "S") ||
(e.key === " " || e.code === "Space")) {
this.keys.splice(this.keys.indexOf(e.key), 1);
}
});
}
}