Pygame Keyboard Input Tutorial for Beginners – Lesson 4

pygame tutorial

In this Pygame Keyboard Input tutorial, you will learn how to control objects using your keyboard.

In real games, players use:

  • Arrow Keys
  • W, A, S, D Keys
  • Spacebar
  • Enter Key

to control their characters.

Today, we will learn how to detect key presses and move a game character around the screen.

By the end of this lesson, you will create your first controllable game object.


What is Keyboard Input in Pygame?

Keyboard input means detecting which key the player presses.

For example:

Right Arrow → Move Right
Left Arrow → Move Left
Up Arrow → Move Up
Down Arrow → Move Down

Almost every game uses keyboard input.

Examples:

  • Racing Games
  • Adventure Games
  • Platform Games
  • Shooting Games
  • Maze Games

Create a New File

Create a file named:

lesson4_keyboard_input.py

Program 1: Detect a Key Press

Type the following code:

import pygame

pygame.init()

screen = pygame.display.set_mode((800,600))

pygame.display.set_caption("Keyboard Input")

running = True

while running:

    screen.fill((255,255,255))

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            print("Key Pressed")

    pygame.display.update()

pygame.quit()

Run the program.

Press any key.

You will see:

Key Pressed

inside the terminal.

Congratulations!

You just detected your first keyboard event.


Understanding KEYDOWN

if event.type == pygame.KEYDOWN:

This means:

“When a key is pressed, do something.”

Think of it like:

Player presses key
↓
Game detects key
↓
Action happens

Program 2: Detect Arrow Keys

Replace the KEYDOWN section with:

if event.type == pygame.KEYDOWN:

    if event.key == pygame.K_RIGHT:
        print("Right Arrow Pressed")

    if event.key == pygame.K_LEFT:
        print("Left Arrow Pressed")

    if event.key == pygame.K_UP:
        print("Up Arrow Pressed")

    if event.key == pygame.K_DOWN:
        print("Down Arrow Pressed")

Run the program.

Press different arrow keys.

You will see messages in the terminal.


Program 3: Move a Rectangle with Arrow Keys

Now let’s make something exciting.

Type the complete program below:

import pygame

pygame.init()

screen = pygame.display.set_mode((800,600))

pygame.display.set_caption("Move the Box")

x = 350
y = 250

running = True

while running:

    screen.fill((255,255,255))

    pygame.draw.rect(screen,(255,0,0),(x,y,100,100))

    keys = pygame.key.get_pressed()

    if keys[pygame.K_RIGHT]:
        x += 5

    if keys[pygame.K_LEFT]:
        x -= 5

    if keys[pygame.K_UP]:
        y -= 5

    if keys[pygame.K_DOWN]:
        y += 5

    pygame.display.update()

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

pygame.quit()

Run the program.

Use the arrow keys.

The red box will move around the screen.

Amazing! 🎮

You just created your first controllable game object.


Understanding get_pressed()

This line is very important:

keys = pygame.key.get_pressed()

It checks which key is currently being pressed.

For example:

if keys[pygame.K_RIGHT]:

means:

If the player is pressing
the Right Arrow key,
move the object right.

How Movement Works

Move Right:

x += 5

Move Left:

x -= 5

Move Down:

y += 5

Move Up:

y -= 5

Remember:

Increase X = Move Right
Decrease X = Move Left
Increase Y = Move Down
Decrease Y = Move Up

Mini Project: Move a Ball

Replace:

pygame.draw.rect()

with:

pygame.draw.circle(screen,(0,0,255),(x,y),50)

Now control a blue ball instead of a rectangle.


Challenge 1

Create a green square.

Move it using arrow keys.


Challenge 2

Change movement speed.

Try:

x += 10

instead of:

x += 5

What happens?


Challenge 3

Create a character that starts in the center of the screen.

Hint:

x = 400y = 300

Common Errors

Why is my object not moving?

Check:

keys = pygame.key.get_pressed()

Make sure it is inside the game loop.


Why does my object disappear?

It may have moved outside the screen.

Try smaller movement values.

Example:

x += 5

instead of:

x += 50

Why are arrow keys not working?

Check spelling carefully.

Correct:

pygame.K_RIGHT

Wrong:

pygame.RIGHT

What You Learned Today

✅ What keyboard input means

✅ How to detect key presses

✅ How to use KEYDOWN

✅ How to use get_pressed()

✅ How to move objects with arrow keys

✅ How real games control characters


Quick Revision

Which function checks pressed keys?

Which key constant represents the Right Arrow key?

What does x += 5 do?

What does y -= 5 do?

Why do games use keyboard input?


What’s Next?

In Lesson 5: Pygame Images and Game Characters, you will learn:

✅ How to load images

✅ How to display a character

✅ How to create a player sprite

✅ How to use PNG images in games

Now your games will start looking like real games instead of simple shapes.


Frequently Asked Questions

How do I move a character in Pygame?

You can move a character by changing its X and Y coordinates when keyboard keys are pressed.

What is Pygame Keyboard Input?

Pygame Keyboard Input allows a game to detect keys pressed by the player and perform actions such as movement, jumping, shooting, or opening menus.

How do I use arrow keys in Pygame?

Use:

pygame.key.get_pressed()

and check:

pygame.K_RIGHT
pygame.K_LEFT
pygame.K_UP
pygame.K_DOWN

to detect arrow key presses.

What is the difference between KEYDOWN and get_pressed()?

KEYDOWN detects when a key is pressed once.

get_pressed() continuously checks whether a key is being held down.

Can I use W, A, S, D keys instead of arrow keys?

Yes.

Example:

pygame.K_wpygame.K_apygame.K_spygame.K_d

These keys are commonly used in computer games.

How do game characters move in Pygame?

Game characters move by changing their X and Y coordinate values every frame.

Why is keyboard input important in game development?

Without keyboard input, players cannot control characters, vehicles, or actions inside a game.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.