I know there are a bunch of other people who have asked the same or similar question but the answers they got hasn't really helped me because, they're pretty complicated and I don't understand it enough to add it to my own code. This was one I have been staring at for a while to try and understand: Javascript on key press trigger only once no luck.
What I trying to get my code to do:
- No matter if I hold or push button do action only once
- Do action on key down and another on key up
- Be able to press multiple keys at the same time
I tried it with true false type variables mixed in:
var map = []; // creation of set
var canKEY = true;
var a = 0;
onkeydown = onkeyup = function(e){ //creation of function
e = e || event; // to deal with IE
map[e.keyCode] = e.type == 'keydown';
/* insert conditional here */
document.getElementById("map").innerHTML = map;
document.getElementById("canKEy").innerHTML = canKEY;
if ((canKEY == true) && map[87] && map[69]){
document.getElementById('document').innerHTML = "forwards up";
canKEY = false;
}
else if ((canKEY == true) && map[87] && map[81]){
document.getElementById('document').innerHTML = "forwards down";
canKEY = false;
}
else if ((canKEY == true) && map[87]){
document.getElementById('document').innerHTML = "forwards";
canKEY = false;
}
else{
document.getElementById('document').innerHTML = "empty";
canKEY = true;
}
}
</script>
You get the point, same thing with the rest of the wasd with qe I can get one command in but then canKEY stay false and I can't input any more commands/
What am I doing wrong?