-
Notifications
You must be signed in to change notification settings - Fork 0
/
p5.input.js
42 lines (39 loc) · 1.19 KB
/
p5.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
window.keysHeld = [];
window.theKey = "";
window.keysLog = [];
window.aKeyPressed = false;
/*String.prototype.replaceAll = function (stringToFind, stringToReplace) {
if (stringToFind === stringToReplace) return this;
var temp = this;
// debug line
// console.log((stringToFind.length > 0) + " and " + temp.includes(stringToFind))
temp = (((stringToFind.length > 0) && temp.includes(stringToFind)) ? (temp.split(stringToFind).join(stringToReplace)) : (temp))
return temp;
};*/
window.isHeld = function(_k) {
return window.keysHeld.includes(_k);
};
window.getKeysHeldAsString = function(d) {
return keysHeld.join(d);
};
document.addEventListener('keydown', function (e) {
window.theKey = e.key;
window.keysLog.push(e.key);
if(isHeld(e.key)) {
return;
}
window.aKeyPressed = true;
window.keysHeld = window.keysHeld.filter(key => (e.key !== key));
window.keysHeld.push(e.key);
// testing
// console.log(window.keysHeld);
}
);
document.addEventListener('keyup', function (e) {
window.theKey = e.key;
window.keysHeld = window.keysHeld.filter(key => (e.key !== key));
if(window.keysHeld.length <= 0) { window.aKeyPressed = false; }
// testing
//console.log(keysHeld);
}
);