diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2016-01-06 21:39:15 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2016-01-06 21:39:15 +0100 |
commit | b7fec57c0463e58f1ad8db126ff4879f59b057d0 (patch) | |
tree | 1fce6d1471b27a91cce16bf42f91af3d6aa9bd1e /src/view/InputHandler.ts | |
parent | c64ead08a36fccc36a42b3e74362aa423cfb62ed (diff) | |
download | rpgedit-b7fec57c0463e58f1ad8db126ff4879f59b057d0.tar rpgedit-b7fec57c0463e58f1ad8db126ff4879f59b057d0.zip |
Start implementing input handling
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; |