Skip to content

Commit

Permalink
feat: add InputController
Browse files Browse the repository at this point in the history
  • Loading branch information
thirdmadman committed Mar 12, 2024
1 parent 801ec40 commit 81a4563
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/app/engine/InputController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export class InputController {
private pressedKeys: { [key: string]: boolean } = {};

private clickedKeys: Array<string> = [];

pushKeyPressed(key: string) {
this.pressedKeys[key] = true;
}

pushKeyReleased(key: string) {
delete this.pressedKeys[key];
if (this.clickedKeys.includes(key)) {
return;
}
this.clickedKeys.push(key);
}

registerKeyListeners() {
document.addEventListener('keydown', (e) => {
this.pushKeyPressed(e.key);
});
document.addEventListener('keyup', (e) => {
this.pushKeyReleased(e.key);
});
}

getPressedKeys() {
return this.pressedKeys;
}

getClickedKeys() {
const copy = [...this.clickedKeys];
this.clickedKeys = [];
return copy;
}
}

0 comments on commit 81a4563

Please sign in to comment.