Learning Goals 3 min
By the end of this lesson you will be able to:
- Drag a when [space v] key pressed block from the Events category onto the Script Area.
- Pick a key from the drop-down (space, arrow keys, or any letter).
- Build two separate scripts on the same sprite — one for the right arrow, one for the left arrow — and steer the cat across the Stage.
Warm-Up 7 min
Last lesson the cat took its first bow when the green flag fired. Now the audience wants to see moves on demand — that means keys! Today you meet a second hat block that listens for a keyboard event instead of the flag.
Quick-fire puzzle
Faiz built this script. He clicked the green flag — the cat did nothing. Then he pressed the space bar — the cat moved! What is going on?
when [space v] key pressed
move (20) steps
Reveal the answer
This script does not listen for the green flag. It listens for the space key. So clicking the flag does nothing — the script is patiently waiting for a finger on the space bar. Every key press makes the cat move 20 steps to the right.
This is the big idea: different hat blocks listen for different events. One script can wait for the flag; another can wait for a key. Same sprite, two ways to wake it up.
New Concept — keys are events too 15 min
Imagine a kitchen doorbell that has two buttons — one rings a high ding, the other rings a low dong. Same kitchen, two bells, two separate sounds. The Scratch keyboard is the same: every key is its own button, with its own scripts waiting behind it.
The new block
| Block | Category | What it does |
|---|---|---|
| when [space v] key pressed | Events (yellow) | Waits for the chosen key to be pressed. Then runs every block snapped underneath. The drop-down lets you pick: space, any arrow key, any letter, any digit. |
Picking a key from the drop-down
Click the small word space inside the block. A long drop-down list appears: space, up arrow, down arrow, right arrow, left arrow, any, a, b, c, …, 0, 1, 2, …, 9. Click one. The block updates to show your choice.
when [right arrow v] key pressed
move (20) steps
Multiple scripts on one sprite
A sprite can have many scripts. Each script has its own hat block. Each hat listens for its own event. That means one cat sprite can react to the flag, the space bar, the right arrow, and the letter m — all at once, all on the same sprite.
To build two scripts on one sprite, drag two separate hat blocks into the Script Area. Leave a gap between them. Snap blocks under each one. Done.
Why it matters
Key-pressed scripts are how every game lets the player control the action. The arrow keys make characters walk. The space bar makes them jump. The letter keys can pick weapons or open doors. Without when [key v] key pressed, your sprites are watching a film. With it, players can step in and play.
Worked Example — steer the cat with the arrow keys 15 min
Open Scratch. We will build two scripts on the cat sprite: the right arrow moves it right, the left arrow moves it left.
Step 1 — Open the Events category
Click the yellow Events dot in the Blocks Panel. You will see all six yellow hat blocks.
Step 2 — Drag the first key-hat
Grab when [space v] key pressed. Drop it in the Script Area. Click on the word space inside the block. Choose right arrow from the drop-down.
Step 3 — Add a move block
Click the blue Motion dot. Drag move (10) steps under the hat. Change the number to 20.
when [right arrow v] key pressed
move (20) steps
Step 4 — Drag a second key-hat below
Go back to Events. Drag another when [space v] key pressed block into the Script Area. Drop it well below the first stack — leave a clear gap so the two scripts do not snap together. Change its drop-down to left arrow.
Step 5 — Add a left-moving Motion block
Drag move (10) steps under the new hat. Change the number to -20 (a negative twenty — that means "move 20 steps backwards", which is left when the cat is facing right).
when [right arrow v] key pressed
move (20) steps
when [left arrow v] key pressed
move (-20) steps
Step 6 — Test your driver
Click on the Stage to make sure it is in focus. Press the right arrow — the cat shifts right. Press the left arrow — the cat shifts left. Tap each key several times. You are now steering the cat with your keyboard.
What changed: last lesson the cat reacted to one event (the flag). Today it reacts to two different keys, each on its own script. The sprite has become interactive.
The full assembled scripts (your reference)
when [right arrow v] key pressed
move (20) steps
when [left arrow v] key pressed
move (-20) steps
Try It Yourself — three small builds 12 min
Goal: Make the space bar make the cat say "Meow!" — without any movement.
when [space v] key pressed
say [Meow!] for (1) seconds
Think: Press the space bar five times in a row. The cat says "Meow!" five times. No need for the green flag at all.
Goal: Add an up arrow that makes the cat point upwards, and a down arrow that makes it point downwards. Pair these with your existing right and left scripts. The cat should now feel like a steerable arrow.
when [up arrow v] key pressed
point in direction (0)
when [down arrow v] key pressed
point in direction (180)
Think: Now the right/left arrows move the cat, and the up/down arrows turn it. Four scripts on one sprite.
Goal: Add a letter b key that bounces the cat off the edge of the Stage. Move the cat right with the right arrow until it touches the edge — then press b to flip its direction.
when [b v] key pressed
if on edge, bounce
Think: The drop-down has every letter and digit, not just space and arrows. Try letter keys that match your sprite's behaviour — j for jump, r for run.
Mini-Challenge — drive Aiman's Cat to the right edge 5 min
"Right-arrow road trip"
Using only the blocks from today (key-hat + motion), build a sprite that the player can drive from the left edge of the Stage to the right edge using only the right arrow key.
It works if:
- Before the test, set the cat's x to
-200in the Sprite Properties panel. - Pressing the right arrow makes the cat take small steps to the right (around 10–20 per press).
- After enough presses, the cat reaches the right edge of the Stage.
- You used at least one when [right arrow v] key pressed hat block.
Reveal one valid solution
Set the cat's starting x to -200. Then build:
when [right arrow v] key pressed
move (10) steps
The Stage is 480 pixels wide. The cat starts at x = -200 and must reach x = +240. That is 440 pixels to cover. At 10 pixels per press, that is about 44 presses. Tap the right arrow 44 times and the cat completes its road trip.
Recap 2 min
Today you met a second hat block — when [space v] key pressed — and learnt that a single sprite can have many scripts, each listening for its own event. The right arrow moves the cat right, the left arrow moves it left, and they live happily side by side on the same sprite.
- when [key v] key pressed (block)
- An Events hat block that listens for a single keyboard key. Pick the key from the block's drop-down.
- Drop-down menu
- A small list inside a block (shown by a tiny ▾). Click it to pick from preset choices — like keys, costumes, or sounds.
- Key event
- The moment a key is pressed on the keyboard. Scripts with a key-hat react to this event.
- Multiple scripts
- A sprite can carry many separate scripts. Each one starts with its own hat block and runs independently.
Homework 1 min
The keyboard cat. Build four key-pressed scripts on one sprite so the cat can be driven by all four arrow keys.
- Open Scratch. Make sure the cat is the only sprite.
- Drag four when [space v] key pressed hat blocks into the Script Area, well apart from each other.
- Set their drop-downs to: right arrow, left arrow, up arrow, down arrow.
- Under each, snap one motion block: right = move (20), left = move (-20), up = change y by (20), down = change y by (-20).
- Tap each key to test. The cat should move in the matching direction.
Bring back next class:
- A screenshot of all four scripts in your Script Area.
- Your written answer: "Why does each arrow need its own hat block? Can you combine all four into a single script?"
Heads up for next class: SCR-L01-17 meets the third hat block — when this sprite clicked. Click the cat on the Stage and watch it react.