44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
'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;
|