summaryrefslogtreecommitdiffstats
path: root/src/view/InputHandler.ts
blob: e39ffe0eb628606165593be9ebb488883672beaf (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
'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) => {
                        this.keys[ev.keyCode] = true;
                        this.callListeners();
                });

                window.addEventListener('keyup', (ev) => {
                        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;