1
\$\begingroup\$

I've been playing around with pygame, and I ran into a bit of an odd issue with input on the pygame event queue. Consider the following test code:

import pygame as pg

def main():
    pg.init()
    pg.display.set_mode([640,480])
    clock = pg.time.Clock()
    while True:
        clock.tick(1)
        events = pg.event.get()
        print("There are %d events in the queue." % len(events))
        for event in events:
            print(event)
        print()


if __name__ == "__main__":
    main()

Here I'm intentionally running the game loop just once per second. What I observe is that the pygame event queue can easily receive dozens of mouse-related events per frame, but only ever a single KEYDOWN or KEYUP event at a time.

This is the output after pressing four keys simultaneously and quickly pressing the left mouse button a few times at the same time:

There are 0 events in the queue.

There are 1 events in the queue.
<Event(5-MouseButtonDown {'pos': (494, 392), 'button': 1})>

There are 8 events in the queue.
<Event(2-KeyDown {'unicode': 'f', 'key': 102, 'mod': 0, 'scancode': 41})>
<Event(6-MouseButtonUp {'pos': (494, 392), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (494, 392), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (494, 392), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (494, 392), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (494, 392), 'button': 1})>
<Event(5-MouseButtonDown {'pos': (494, 392), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (494, 392), 'button': 1})>

There are 3 events in the queue.
<Event(2-KeyDown {'unicode': 'q', 'key': 113, 'mod': 0, 'scancode': 24})>
<Event(5-MouseButtonDown {'pos': (494, 392), 'button': 1})>
<Event(6-MouseButtonUp {'pos': (494, 392), 'button': 1})>

There are 1 events in the queue.
<Event(2-KeyDown {'unicode': 'e', 'key': 101, 'mod': 0, 'scancode': 26})>

There are 1 events in the queue.
<Event(2-KeyDown {'unicode': 'w', 'key': 119, 'mod': 0, 'scancode': 25})>

There are 1 events in the queue.
<Event(3-KeyUp {'key': 119, 'mod': 0, 'scancode': 25})>

There are 1 events in the queue.
<Event(3-KeyUp {'key': 113, 'mod': 0, 'scancode': 24})>

There are 1 events in the queue.
<Event(3-KeyUp {'key': 101, 'mod': 0, 'scancode': 26})>

There are 1 events in the queue.
<Event(3-KeyUp {'key': 102, 'mod': 0, 'scancode': 41})>

There are 0 events in the queue.

At 1 FPS, it takes pygame 8 seconds to process just 4 keypresses, one for each KEYUP and KEYDOWN signal. Is this intended behavior? Am I missing something here? It seems odd to me that pygame cannot register multiple keypresses per frame. I'm a bit of a newbie when it comes to gamedev, so I'm not sure where this is coming from.

\$\endgroup\$
  • \$\begingroup\$ I can't reproduce this behavior. The events appear in the next frame for me. What OS, Python and pygame version do you use? \$\endgroup\$ – skrx Mar 5 '18 at 8:21
  • 1
    \$\begingroup\$ @skrx I'm working on Linux (Fedora 27, kernel 4.15.6) using python 3.6.4 with pygame 1.9.3. I just tested it on a Windows 7 installation a while ago on another PC and there it was indeed working fine (I should have probably done that sooner, my bad). Anyway, that gives me a starting point to test this on a few different setups and hopefully track down the source of the problem. Thanks for taking the time to test it. \$\endgroup\$ – user1064701 Mar 5 '18 at 14:57
  • \$\begingroup\$ Better post this to the mailing list to contact the developers. \$\endgroup\$ – skrx Mar 6 '18 at 2:42
1
\$\begingroup\$

Please use the function pygame.key.set_repeat(delay, interval) once before starting the infinite while loop. I use as delay 1 and an interval of 25.

From the docs: pygame.key module

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. When pygame is initialized the key repeat is disabled.

This is the equivalent of for example a key pressed event in Game Maker

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.