0

I have a doubt and I can't find this info. To manage keyboard inputs in games, I listen to keydown and keyup events with addEventListener() to update the state of a keyboard object, so in the game loop I can read if a key is down or not. Of course I want it to run between the game loops, but does it ? Or does it run at the very moment the key is pressed or released no matter if the game loop is running ?

document.body.addEventListener( "keydown", function(){/*function that updates my keyboard object*/} , true);
document.body.addEventListener( "keyup", function(){/*function that updates my keyboard object*/} , true);
function game_loop()
{ requestAnimationFrame(game_loop); }
game_loop();
Diego
  • 569
  • 1
  • 9
  • 28
  • Please show us your code and explain what's wrong or the issues you're having with it – Alon Eitan May 14 '17 at 17:42
  • 1
    It runs when there is no other code running. If the key is pressed while you have JavaScript code running, then the currently running code must first run to completion (with an empty call stack at the end) before the events are processed and listeners are called. – trincot May 14 '17 at 17:44

0 Answers0