title: PyGame not receiving events when 3+ keys are pressed at the same time
body: <p><em>I am developing a simple game in <a href="http://www.pygame.org/" rel="nofollow noreferrer">PyGame</a>...  A rocket ship flying around and shooting stuff.</em>  </p>
  
  <hr>
  
  <p><strong>Question:</strong> Why does pygame stop emitting keyboard events when too may keys are pressed at once?</p>
  
  <p><strong>About the Key Handling:</strong> The program has a number of variables like <code>KEYSTATE_FIRE, KEYSTATE_TURNLEFT</code>, etc...</p>
  
  <ol>
  <li>When a <code>KEYDOWN</code> event is handled, it sets the corresponding <code>KEYSTATE_*</code> variable to True.</li>
  <li>When a <code>KEYUP</code> event is handled, it sets the same variable to False.</li>
  </ol>
  
  <p><strong>The problem:</strong>
  If <code>UP-ARROW</code> and <code>LEFT-ARROW</code> are being pressed at the same time, pygame DOES NOT emit a <code>KEYDOWN</code> event when <code>SPACE</code> is pressed.  This behavior varies depending on the keys.  When pressing letters, it seems that I can hold about 5 of them before pygame stops emitting <code>KEYDOWN</code> events for additional keys.</p>
  
  <p><strong>Verification:</strong> In my main loop, I simply printed each event received to verify the above behavior.</p>
  
  <p><strong>The code:</strong> For reference, here is the (crude) way of handling key events at this point:</p>
  
  <pre><code>while GAME_RUNNING:
      FRAME_NUMBER += 1
      CLOCK.tick(FRAME_PER_SECOND)
  
      #----------------------------------------------------------------------
      # Check for events
      for event in pygame.event.get():
          print event
  
          if event.type == pygame.QUIT:
              raise SystemExit()
  
          elif event.type == pygame.KEYDOWN and event.dict['key'] == pygame.K_UP:
              KEYSTATE_FORWARD = True
          elif event.type == pygame.KEYUP and event.dict['key'] == pygame.K_UP:
              KEYSTATE_FORWARD = False
  
          elif event.type == pygame.KEYDOWN and event.dict['key'] == pygame.K_DOWN:
              KEYSTATE_BACKWARD = True
          elif event.type == pygame.KEYUP and event.dict['key'] == pygame.K_DOWN:
              KEYSTATE_BACKWARD = False
  
          elif event.type == pygame.KEYDOWN and event.dict['key'] == pygame.K_LEFT:
              KEYSTATE_TURNLEFT = True
          elif event.type == pygame.KEYUP and event.dict['key'] == pygame.K_LEFT:
              KEYSTATE_TURNLEFT = False
  
          elif event.type == pygame.KEYDOWN and event.dict['key'] == pygame.K_RIGHT:
              KEYSTATE_TURNRIGHT = True
          elif event.type == pygame.KEYUP and event.dict['key'] == pygame.K_RIGHT:
              KEYSTATE_TURNRIGHT = False
  
          elif event.type == pygame.KEYDOWN and event.dict['key'] == pygame.K_SPACE:
              KEYSTATE_FIRE = True
          elif event.type == pygame.KEYUP and event.dict['key'] == pygame.K_SPACE:
              KEYSTATE_FIRE = False
  
      # remainder of game loop here...
  </code></pre>
  
  <p><strong>For pressing this sequence:</strong></p>
  
  <ul>
  <li><code>a (down)</code></li>
  <li><code>s (down)</code></li>
  <li><code>d (down)</code> </li>
  <li><code>f (down)</code> </li>
  <li><code>g (down)</code></li>
  <li><code>h (down)</code></li>
  <li><code>j (down)</code></li>
  <li><code>k (down)</code></li>
  <li><code>a (up)</code></li>
  <li><code>s (up)</code></li>
  <li><code>d (up)</code></li>
  <li><code>f (up)</code></li>
  <li><code>g (up)</code></li>
  <li><code>h (up)</code></li>
  <li><code>j (up)</code></li>
  <li><code>k (up)</code></li>
  </ul>
  
  <p><strong>Here is the output:</strong></p>
  
  <ul>
  <li><code>&lt;Event(2-KeyDown {'scancode': 30, 'key': 97, 'unicode': u'a', 'mod': 0})&gt;</code></li>
  <li><code>&lt;Event(2-KeyDown {'scancode': 31, 'key': 115, 'unicode': u's', 'mod': 0})&gt;</code></li>
  <li><code>&lt;Event(2-KeyDown {'scancode': 32, 'key': 100, 'unicode': u'd', 'mod': 0})&gt;</code></li>
  <li><code>&lt;Event(2-KeyDown {'scancode': 33, 'key': 102, 'unicode': u'f', 'mod': 0})&gt;</code></li>
  <li><code>&lt;Event(3-KeyUp {'scancode': 30, 'key': 97, 'mod': 0})&gt;</code></li>
  <li><code>&lt;Event(3-KeyUp {'scancode': 31, 'key': 115, 'mod': 0})&gt;</code></li>
  <li><code>&lt;Event(3-KeyUp {'scancode': 32, 'key': 100, 'mod': 0})&gt;</code></li>
  <li><code>&lt;Event(3-KeyUp {'scancode': 33, 'key': 102, 'mod': 0})&gt;</code></li>
  <li><code>&lt;Event(2-KeyDown {'scancode': 36, 'key': 106, 'unicode': u'j', 'mod': 0})&gt;</code></li>
  <li><code>&lt;Event(2-KeyDown {'scancode': 37, 'key': 107, 'unicode': u'k', 'mod': 0})&gt;</code></li>
  <li><code>&lt;Event(3-KeyUp {'scancode': 36, 'key': 106, 'mod': 0})&gt;</code></li>
  <li><code>&lt;Event(3-KeyUp {'scancode': 37, 'key': 107, 'mod': 0})&gt;</code></li>
  </ul>
  
  <hr>
  
  <p>Is this a common issue?  Is there a workaround?  If not, what is the best way to handle multiple-key control issues when using pygame?</p>