witchday/KeyPressListener.js
2024-08-05 19:36:41 +02:00

26 lines
602 B
JavaScript

class KeyPressListener {
constructor(keyCode, callback) {
let keySafe = true;
this.keydownFunction = function(event) {
if (event.code === keyCode) {
if (keySafe) {
keySafe = false;
callback();
}
}
};
this.keyupFunction = function(event) {
if (event.code === keyCode) {
keySafe = true;
}
};
document.addEventListener("keydown", this.keydownFunction);
document.addEventListener("keyup", this.keyupFunction);
}
unbind() {
document.removeEventListener("keydown", this.keydownFunction);
document.removeEventListener("keyup", this.keyupFunction);
}
}