-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.js
53 lines (42 loc) · 1.49 KB
/
input.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class InputHandler {
constructor(keyMap) {
this.directInterface = {}; // direct layer from input
this.inputs = {}; // reformats direct layer to be usable
this.changeInputs(keyMap);
for (let i in this.keyMapKeys) {
this.directInterface[this.keyMapKeys[i]] = null;
this.inputs[keyMap[this.keyMapKeys[i]]] = null;
}
}
changeInputs(keyMap) {
this.keyMap = keyMap;
this.keyMapKeys = Object.keys(this.keyMap); // all keys
}
keyDown(e) {
if (!e.repeat) { // this function will also be called bc of built-in key ARR
this.directInterface[e.code] = Date.now(); // the code for the keyDown is here
// console.log(this.directInterface);
}
}
keyUp(e) {
this.directInterface[e.code] = null; // the code for the keyUp is here
}
getInputs() {
for (let i in this.keyMapKeys) {
this.inputs[this.keyMap[this.keyMapKeys[i]]] = null;
}
for (let i in this.keyMapKeys) {
i = this.keyMapKeys[i];
let j = this.keyMap[i];
// ternary statement separated for brevity
if (this.directInterface[i] !== null) { // if it's not undefined (pushed down)
const holdTime = Date.now() - this.directInterface[i]; // amount of time held down
if (this.inputs[j] === null || this.inputs[j] > holdTime) { // if it's undefined or greater than holdTime
this.inputs[j] = holdTime; // set it to holdTime
}
}
}
return this.inputs;
}
}
export { InputHandler };