blob: dcfd85d73e90042ce2dd961401ae675771fc1495 (
plain)
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
|
'use strict';
import Direction from '../model/Direction';
class InputHandler {
keys: {[key: number]: boolean} = {};
listeners: (() => void)[] = [];
private callListeners() {
this.listeners.forEach(l => l());
}
constructor() {
window.addEventListener('keydown', (ev) => {
if (!this.keys[ev.keyCode]) {
this.keys[ev.keyCode] = true;
this.callListeners();
}
});
window.addEventListener('keyup', (ev) => {
if (this.keys[ev.keyCode]) {
delete this.keys[ev.keyCode];
this.callListeners();
}
});
}
addListener(l: () => void) {
this.listeners.push(l);
}
}
module InputHandler {
export const Left = 37;
export const Up = 38;
export const Right = 39;
export const Down = 40;
};
export default InputHandler;
|