Javascript key down only firing once when key is held down -
this question has answer here:
i'm using following code load page whenever w,s,a,d keys 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 contain url address.
my question is, possible have keypress register once per click? @ moment if hold key down fires rapidly.
thanks tips.
try 'keyup' event:
<script> document.addeventlistener('keyup', 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>
Comments
Post a Comment