blob: d06bcf8862375c99c7ed94d5b7dcde7a39353016 (
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
45
46
47
48
49
50
51
52
53
54
|
'use strict';
import Direction from '../model/Direction';
class InputHandler {
keys: {[key: number]: boolean} = {};
private listeners: (() => void)[] = [];
private callListeners(): void {
this.listeners.forEach(l => l());
}
constructor(private relevantKeys: {[key: number]: boolean}) {
window.addEventListener('keydown', (ev) => {
if (!relevantKeys[ev.keyCode])
return;
ev.preventDefault();
if (!this.keys[ev.keyCode]) {
this.keys[ev.keyCode] = true;
this.callListeners();
}
});
window.addEventListener('keyup', (ev) => {
if (!relevantKeys[ev.keyCode])
return;
ev.preventDefault();
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;
|