Pygame modules for interactive programs
Other Input Modules
Each major input device (keyboard, mouse, joystick) has its own associated Pygame module. Although it's possible to connect multiple keyboards and mice, the Linux input drivers will treat them all as one device. Joysticks, however, are initialized by Pygame directly, and each one responds separately. I'll look at these input devices individually and show how each type interacts with Pygame.
Key Presses and Releases
Although key press and release events don't require any special setup to appear on the event queue, the pygame.key
module has functions to modify exactly how key events appear on the queue.
By default, Pygame does not repeat key presses, but pygame.key.set_repeat()
takes two arguments and will enable key repeat. The first argument is the initial delay, and the second argument is the delay between repeats. Each time a repeat occurs, a new pygame.KEYDOWN
event appears on the event queue.
Another useful function is pygame.key.get_focused()
. If the function returns True
, then Pygame is currently receiving keyboard input. By checking this function inside the event loop, you can pause your game automatically – for example, if focus switches away from the program.
Most of the other example programs involve key press events, so you can refer to them for examples of how to process keyboard events.
Buy this article as PDF
Pages: 2
(incl. VAT)