-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboardController.js
62 lines (54 loc) · 1.45 KB
/
KeyboardController.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
class KeyboardController {
constructor(target) {
this.target = target;
this.up = false;
this.down = false;
this.left = false;
this.right = false;
this.atira = false;
this.onKeyDown = (event) => {
return this.handler(event, true);
};
this.onKeyUp = (event) => {
return this.handler(event, false);
};
}
bind() {
this.target.addEventListener('keyup', this.onKeyUp);
this.target.addEventListener('keydown', this.onKeyDown);
}
unbind() {
this.target.removeEventListener('keyup', this.onKeyUp);
this.target.removeEventListener('keydown', this.onKeyDown);
}
reset() {
this.left = false;
this.right = false;
this.up = false;
this.down = false;
this.atira = false;
}
handler(event, status) {
switch (event.key) {
case 'ArrowDown':
this.down = status;
break;
case 'ArrowRight':
this.right = status;
break;
case 'ArrowLeft':
this.left = status;
break;
case 'ArrowUp':
this.up = status;
break;
case ' ':
this.atira = status;
break;
default:
return true;
}
event.preventDefault();
return false;
}
}