-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyPressListener.js
32 lines (25 loc) · 943 Bytes
/
KeyPressListener.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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("keyup", this.keyupFunction);
document.removeEventListener("keydown", this.keydownFunction);
}
}
// this is code to have a certain key pressed and for it to fire off once and not again until its released and this will give it more of that type of old gameboy feel