zippy-game-engine
Version:
A lightweight game engine for web games
32 lines • 993 B
JavaScript
export class KeyboardSystem {
keys;
boundKeyDown;
boundKeyUp;
constructor() {
this.keys = {};
this.boundKeyDown = this.#handleKeyDown.bind(this);
this.boundKeyUp = this.#handleKeyUp.bind(this);
this.#setupEventListeners();
}
isKeyDown(key) {
return this.keys[key] || false;
}
destroy() {
window.removeEventListener("keydown", this.boundKeyDown);
window.removeEventListener("keyup", this.boundKeyUp);
}
#setupEventListeners() {
window.addEventListener("keydown", this.boundKeyDown);
window.addEventListener("keyup", this.boundKeyUp);
}
#handleKeyDown(e) {
this.keys[e.key] = true;
if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", " "].includes(e.key)) {
e.preventDefault(); // Prevent scrolling for arrow keys/space
}
}
#handleKeyUp(e) {
this.keys[e.key] = false;
}
}
//# sourceMappingURL=keyboard-system.js.map