-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeysDownLib.js
More file actions
26 lines (26 loc) · 865 Bytes
/
keysDownLib.js
File metadata and controls
26 lines (26 loc) · 865 Bytes
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
keyHandler = {
keysDown: [],
init: function () {
document.addEventListener("keydown", function (e) {
keyHandler.addKey(e.code);
})
document.addEventListener("keyup", function (e) {
keyHandler.removeKey(e.code);
})
console.log("KeysDownLibrary has been initialized")
},
addKey: function (e) {
if (!this.keysDown.includes(e)) {
this.keysDown.push(e);
}
},
removeKey: function (e) {
if (this.keysDown.includes(e)) {
// The line below splices the element in the array at the index of the key that was released. The 1 meanns only remove 1 element.
this.keysDown.splice(this.keysDown.indexOf(e), 1);
}
},
isHeld: function (key) {
return this.keysDown.includes(key)
}
}