Menu

Pygame Keyboard Input: KEYDOWN and key.get_pressed()

Read the keyboard in pygame two ways: KEYDOWN events for single presses such as jumping, and pygame.key.get_pressed() for keys held down such as walking. With key constants, WASD and arrow keys, modifiers and typed text.

This page includes runnable editors. Edit, run, and see output instantly.

Pygame gives you the keyboard in two forms, and most games use both:

  • KEYDOWN events: "this key was just pressed." Once per press. Good for jump, shoot, pause.
  • pygame.key.get_pressed(): "these keys are down right now." Every frame while held. Good for walking.

This game uses both: arrows (or WASD) walk, Space jumps.

Press Play, click the game, then walk with the arrow keys and jump with Space. Hold Space: the player jumps only once, because the jump is on the KEYDOWN event.

Held keys: get_pressed()

pygame.key.get_pressed() returns a sequence you index with key constants. A pressed key is True:

keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
    player.y -= 5

Call it once per frame, after the event loop. Because True counts as 1, the difference of two keys gives a direction:

dx = keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]   # -1, 0 or 1

Single presses: KEYDOWN

A KEYDOWN event has:

AttributeValue
event.keyThe key constant, such as pygame.K_SPACE
event.unicodeThe character it typed, such as "a" or "A"; empty for arrows
event.modModifier keys held at the time (Shift, Ctrl, Alt)

KEYUP fires when the key is released, which is how you make a variable jump (cut the jump short when Space comes up). pygame-ce also has pygame.key.get_just_pressed() and get_just_released(), which return a state like get_pressed() but only for keys that changed this frame.

Key constants

KeysConstants
LettersK_a to K_z (lowercase, even with Shift)
DigitsK_0 to K_9
ArrowsK_LEFT, K_RIGHT, K_UP, K_DOWN
CommonK_SPACE, K_RETURN, K_ESCAPE, K_BACKSPACE, K_TAB
ModifiersK_LSHIFT, K_RSHIFT, K_LCTRL, K_RCTRL, K_LALT, K_RALT
Function keysK_F1 to K_F12

pygame.key.name(pygame.K_LEFT) returns "left", which is useful for a controls screen.

Shift, Ctrl and Alt

Check modifiers with a bitwise and on event.mod (or on pygame.key.get_mods()):

if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
    if event.mod & pygame.KMOD_CTRL:
        save_game()

KMOD_SHIFT, KMOD_CTRL and KMOD_ALT match either the left or the right key.

Typing text

For a name entry box, add event.unicode on each KEYDOWN and handle Backspace:

Click the game, type a name and press Enter. A full text field in a desktop game would read the TEXTINPUT event, which also handles accents and other keyboard layouts.

Frequently Asked Questions

How do I check if a key is held down in pygame?

Call keys = pygame.key.get_pressed() once per frame, then test keys[pygame.K_LEFT], which is true while the key is down. Use it for movement.

How do I detect a single key press?

Use the event: if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:. It fires once per press, which is right for jumping, shooting or opening a menu. In pygame-ce, pygame.key.get_just_pressed() gives the same answer as a state you can index.

What are the pygame key names?

Constants that start with K_: K_a to K_z, K_0 to K_9, K_SPACE, K_RETURN, K_ESCAPE, K_LEFT, K_RIGHT, K_UP, K_DOWN, K_LSHIFT and so on. pygame.key.name(key) turns a constant back into a readable name.

Why does holding a key only move my player once?

You are moving on the KEYDOWN event, which fires once per press. Move with pygame.key.get_pressed() instead, which is true for every frame the key is held.

Coddy programming languages illustration

Learn to code with Coddy

Get started