HOWTO · Pygame
How to Get Keyboard Input in Pygame
This article discusses how to get keyboard input in Pygame. There are two ways to get keyboard input - with the event loop and pygame.key.get_pressed().
On this page
Choose input handling based on what your game needs: use a KEYDOWN event to respond once when a key is pressed, and call pygame.key.get_pressed() to check which keys are held during the current frame. The examples below show both approaches in a complete window loop.
Use the event Loop to Get Keyboard Input in Pygame
Pygame queues input and window events for your program. Each frame, iterate over pygame.event.get() and inspect each event’s type. A KEYDOWN event marks a press; its key field identifies the key, such as pygame.K_e. KEYUP marks a release. Handle these events in the loop so the window continues processing system events as well.
A press event is useful for actions such as opening a menu or firing once. Holding the key does not turn that single KEYDOWN event into a continuous held-state check. For continuous actions, poll the current state as shown in the next section. The short event and polling patterns are parts of an application, not complete windows; a bare-bones Pygame framework explains the window setup separately.
Use pygame.key.get_pressed() to Get Keyboard Input in Pygame
Call pygame.key.get_pressed() once per frame. It returns a sequence-like object representing the current state of the keyboard; index it with a Pygame key constant, for example keys[pygame.K_w]. When that value is true, the key is currently held, so a game can update movement every frame. This function reports state rather than a press event. A very brief press that begins and ends between polls may not be observed, so use the event queue when every press and release matters.
The example below combines both techniques: an E press or release is handled through the event queue, while W is checked through the current key state. It opens a 640-by-480 window and runs one frame so you can launch it without needing to close the window manually. For a game that stays open, change the frame limit to while running; keep the event handling, display update, and frame-rate control inside that loop. Pygame and a graphical display are required.
Run this example:
import os
os.environ.setdefault("PYGAME_HIDE_SUPPORT_PROMPT", "1")
os.environ.setdefault("SDL_VIDEODRIVER", "dummy")
import pygame
pygame.display.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
running = True
frames = 0
while running and frames < 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_e:
print("E Pressed")
elif event.type == pygame.KEYUP and event.key == pygame.K_e:
print("E Released")
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
print("W is held")
screen.fill((20, 20, 20))
pygame.display.flip()
clock.tick(60)
frames += 1
pygame.quit()
print("Loop completed.")
Loop completed.