title: Running code repeatedly while key pressed in JavaScript
body: <p>I am trying to create a game in JavaScript with THREE.js. I am currently trying to figure out how to properly process keyboard input.</p>
  
  <p>I have already tried a few ways to detect and process keyboard events so that my game properly responds to key down and up events immediately, however my attempts seem to cause a huge performance hit or delay.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>window.addEventListener('keydown', onKeypress);
  
  function onKeypress(event) {
    switch (event.key) {
      case "ArrowUp":
        //Code to execute
        break;
      case "ArrowDown":
        //Code to execute
        break;
      case "ArrowLeft":
        //Code to execute
        break;
      case "ArrowRight":
        //Code to execute
        break;
    }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I would like this to be a continuous and smooth loop while the key is pressed, but instead I only receive one keydown event and then a few seconds later I get the repeated keys. Does anyone know how I could do this in such a way that I get a continuous smooth stream of the function being called?</p>
  
  <p><a href="https://pastebin.com/hcFmD4ez" rel="nofollow noreferrer">Rest of code here</a></p>