title: How to detect a held key in a flash game?
body: <p>What is the correct way to detect held keys in a flash game?
  For example, I want to know that the right arrow is held to move the player.</p>
  
  <p><strong>Naive code:</strong></p>
  
  <pre><code>function handleKeyDown(event:KeyboardEvent) {
      held[event.keyCode] = true;
  }
  
  function handleKeyUp(event:KeyboardEvent) {
      held[event.keyCode] = false;
  }
  
  stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
  stage.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
  </code></pre>
  
  <p>The naive code <strong>has problems</strong> on some computers.
  The KEY_DOWN event is alternating with KEY_UP many times for a held key there.
  That makes the key appear to be released in some frames.</p>
  
  <p>An example of the seen events:</p>
  
  <pre><code>[Just holding a single key.]
  KEY_DOWN,KEY_UP,KEY_DOWN,KEY_UP,KEY_DOWN,KEY_UP,...
  </code></pre>