Basic input with keydown/keyup in Python
<p>i am currently trying to create a little game in python, but as i try to work with keydown/keyup events my system would interpret both events as one. I wrote a simple script to monitor the events that are created by 'pygame'(a module to simplify making games in python) and as i press down a key it instantly shows both the keydown and keyup event in the python shell: <a href="http://i.stack.imgur.com/tc5Xx.png" rel="nofollow">code screenshot</a>. Is this related to my keyboard?</p>

<p>Your issue with the KEYUP event immediately after the the KEYDOWN event is caused by the line <code>display = pygame.display.set_mode((800, 600))</code> being within your main loop. The program is creating a new display on every iteration of the loop. This line, needs to be placed before the main loop as it should be placed before the main loop. This is the same with <code>pygame.display.set_caption()</code> as well, as it only needs to be called once as well.</p>

<p>Doing that should clear up your issue with the KEYUP event; however, holding down the key still will not work as key repeat is disabled by default when Pygame is initialized. To enable it, the method <code>pygame.key.set_repeat(delay, interval)</code> must be called. This should also go before your main loop.</p>

<p>In the method the value <code>delay</code> is the delay before the first repeat of the key-press will happen in milliseconds, and if it is set to zero, key repeat will be disabled. The value <code>interval</code> is the time between each consecutive repeat after the first. The documentation for this can be found <a href="http://www.pygame.org/docs/ref/key.html#pygame.key.set_repeat" rel="nofollow">here in the Pygame documantation</a> where the description is as follows:</p>

<blockquote>
  <p>When the keyboard repeat is enabled, keys that are held down will generate multiple pygame.KEYDOWN events. The delay is the number of milliseconds before the first repeated pygame.KEYDOWN will be sent. After that another pygame.KEYDOWN will be sent every interval milliseconds. If no arguments are passed the key repeat is disabled.</p>
  
  <p>When pygame is initialized the key repeat is disabled.</p>
</blockquote>

<p>As an example: <code>pygame.key.set_repeat(1, 15)</code> will have essentially no delay and should provide a mostly smooth travel.</p>

python events pygame