-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyHandler.js
More file actions
29 lines (26 loc) · 786 Bytes
/
KeyHandler.js
File metadata and controls
29 lines (26 loc) · 786 Bytes
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
class KeyHandler {
constructor({ player1, player2 }) {
this.player1 = player1;
this.player2 = player2;
}
attach() {
document.body.onkeydown = this.keyHandler.bind(this);
document.body.onkeyup = this.keyHandler.bind(this);
}
// When the player presses the arrow keys, set the corresponding flag in the client.
keyHandler(e) {
e = e || window.event;
if (e.keyCode == 39) {
this.player1.key_right = e.type == "keydown";
} else if (e.keyCode == 37) {
this.player1.key_left = e.type == "keydown";
} else if (e.key == "d") {
this.player2.key_right = e.type == "keydown";
} else if (e.key == "a") {
this.player2.key_left = e.type == "keydown";
} else {
console.log(e);
}
}
}
export default KeyHandler;