← QCE Digital Solutions Unit 1

Responding to keyboard events

Links a key press to a handler function. Each time a key is pressed, tkinter calls handler(event), and event.keysym tells you which key it was.

Understanding Responding to keyboard events in QCE Digital Solutions Unit 1

What it does: Tkinter calls handler(event), so the handler needs one parameter (here event) to receive the key press details. Comparing event.keysym with a key name inside the handler lets the program react to one particular key. Pass the function name without brackets. Brackets would call on_key straight away instead of letting tkinter call it when a key is pressed. For the arrow keys, keysym is Up, Down, Left or Right. Use == to compare values. A single = is assignment and causes a syntax error in a condition. The handler is an event handler. It waits quietly and runs every time the event happens.

Common mistakes: A function header must end with a colon. The first line should be def key(event): so the handler body can follow. Python is case sensitive. The window class is Tk with a capital T, so it must be root = tk.Tk(). Without it, root.bind() has no window to work on.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 158–380) and word search (Modules 82–380).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line makes tkinter call on_key every time any key is pressed?
root.bind('<KeyPress>', on_key)root.bind('<KeyPress>', on_key())
Matches
root.bind('<KeyPress>', on_key())
↓
The handler is called with brackets, so it runs straight away instead of waiting for a key press. Pass on_key without ().
Applying
Meanings
Term
root.bind('<KeyPress>', handler)
Meaning
Links a key press to a handler function. Each time a key is pressed, tkinter calls handler(event), and event.keysym tells you which key it was.
Practice
import tkinter as tk
Testing
Gaps
root.('<KeyPress>', handler)
Check Answer
Spots
1def key(event)
2 print(event.keysym)

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Desert Tortoise' badge — Desert Master, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line makes tkinter call on_key every time any key is pressed?
A. root.bind('<KeyPress>', on_key) ✓
B. root.bind('<KeyPress>', on_key())
Pass the function name without brackets. Brackets would call on_key straight away instead of letting tkinter call it when a key is pressed.
2. A handler for a key press needs which parameter?
A. def on_key(event): ✓
B. def on_key():
tkinter passes an event object to the handler, so the function must have a parameter to receive it.
3. Which expression tells you the name of the key that was pressed?
A. root.keysym
B. event.keysym ✓
The key name is stored on the event object that tkinter passes to the handler, not on the window.
4. A player presses the Up arrow key. What is event.keysym?
A. 'Arrow'
B. 'Up' ✓
For the arrow keys, keysym is Up, Down, Left or Right.
5. Which condition checks that the Escape key was pressed inside the handler?
A. if event.keysym == 'Escape': ✓
B. if event.keysym = 'Escape':
Use == to compare values. A single = is assignment and causes a syntax error in a condition.

Match the Pairs

root.bind('<KeyPress>', on_key) → Tells tkinter to call the function on_key every time any key is pressed in the window
event.keysym → Gives the name of the key that was pressed, such as 'a', 'Up' or 'space'
A handler function must have a parameter to receive the event that tkinter passes in → def on_key(event):
Respond only when a specific key, such as the Up arrow, is pressed → if event.keysym == 'Up':
root.bind('<KeyPress>', on_key()) → The handler is called with brackets, so it runs straight away instead of waiting for a key press. Pass on_key without ().
def on_key(): print(event.keysym) → The handler has no event parameter, so event is undefined and tkinter cannot pass the key press in. It should be def on_key(event):

Key Term

root.bind('<KeyPress>', handler)
Links a key press to a handler function. Each time a key is pressed, tkinter calls handler(event), and event.keysym tells you which key it was.

Fill in the Blanks

1. root.bind('<KeyPress>', handler)
Hint: Type the method that links an event to a handler function
2. def handler(event): print(event.keysym)
Hint: Type the parameter name that receives the details of the key press
3. key = event.keysym
Hint: Type the attribute that holds the name of the key pressed
4. if event.keysym == 'Up': print('Up arrow')
Hint: Type the operator that checks whether the key name equals a value
5. def press(event): print(event.keysym) root.bind('<KeyPress>', press)
Hint: Type the name of the function defined above, without brackets

Spot the Error

Question 1
A def key(event)
B print(event.keysym)
C root.bind('<KeyPress>', key)
Line A — A function header must end with a colon. The first line should be def key(event): so the handler body can follow.
Question 2
A import tkinter as tk
B root = tk.tk()
C def key(event):
D print(event.keysym)
E root.bind('<KeyPress>', key)
Line B — Python is case sensitive. The window class is Tk with a capital T, so it must be root = tk.Tk(). Without it, root.bind() has no window to work on.
Question 3
A root = tk.Tk()
B def key(event):
C print(event.keysym)
D root.bind('<KeyPress>', key())
E root.mainloop()
Line D — bind() needs the handler function itself, not the result of calling it. Writing key() runs it straight away. It should be root.bind('<KeyPress>', key).
Question 4
A def key(event):
B if event.keysym == 'Up':
C print('Moving up'
D root.bind('<KeyPress>', key)
Line C — The print call is missing its closing bracket, which causes a SyntaxError. It should be print('Moving up').
Question 5
A def key(event):
B pressed = event.keysym
C if pressed = 'space':
D print('Jump!')
E root.bind('<KeyPress>', key)
Line C — A single = assigns a value, but an if condition needs the comparison operator ==. It should be if pressed == 'space':.

Student with a class code? Join your class →