I have implemented the code below to redirect to a specific page when the w, s, a, d keys are pressed.
<script>
document.addEventListener('keydown', function(e){
e = e || window.event;
key = e.keyCode || e.charCode;
var keys = {
87: '{$up}',
68: '{$right}',
83: '{$down}',
65: '{$left}'
};
if (keys[key]) window.location.href = keys[key];
});
</script>
The PHP variables store the URL addresses.
My query is whether it is feasible to make the keypress register only once per click. Currently, if you hold the key down, it triggers repeatedly.
Appreciate any suggestions or advice.