title: Count how many keys are pressed at the same time in JavaScript
body: <p>Is it possible to count how many keys are pressed at the same time? I've come quite far but I get some unwanted behavior when tabbing away from my browser window. If I hold down x number of keys when tabbing away from the browser window and then release the keys in a different window the numKeys array stays the same when I tab back to the browser window. Maybe you can check how many keys are pressed when returning to the browser window or something?</p>
  
  <p>My code so far:</p>
  
  <pre><code>var numKeys = [];
  
  $(document).keydown(function(e){
      if(numKeys.indexOf(e.keyCode) == -1){
          numKeys.push(e.keyCode);
      }
      document.getElementById('test').innerHTML = numKeys.length;
  });
  
  $(document).keyup(function(e){
      if(numKeys.indexOf(e.keyCode) &gt; -1){
          var index = numKeys.indexOf(e.keyCode);
          if(index &gt; -1){ numKeys.splice(index, 1); }
      }
      document.getElementById('test').innerHTML = numKeys.length;
  });
  </code></pre>