diff options
Diffstat (limited to 'src/view/InputHandler.ts')
-rw-r--r-- | src/view/InputHandler.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/view/InputHandler.ts b/src/view/InputHandler.ts new file mode 100644 index 0000000..e39ffe0 --- /dev/null +++ b/src/view/InputHandler.ts @@ -0,0 +1,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; |