Learning Goals 3 min
By the end of this lesson you will be able to:
- Tell apart the two Scratch keyboard blocks: the yellow hat when [space v] key pressed (fires once per press) and the blue boolean <key [space v] pressed?> (true while held).
- Open the key dropdown on either block and pick from any, space, the four arrows, the letters a..z, and the digits 0..9 — and explain why "any" is sometimes the right choice.
- Decide which block fits a job: hat blocks for "trigger an action" (jump, shoot, change costume); boolean-in-a-loop for "while held, do X" (walk smoothly, hold to charge, hold to dim).
Warm-Up — two cats, one keyboard 7 min
In Level 1 you used when [space v] key pressed to start a script. It's a hat block — it sits at the top and waits, like the flag hat. Today we meet its twin, the boolean reporter <key [space v] pressed?>, which lives in the Sensing palette. Same word, very different feel.
when [right arrow v] key pressed
move (10) steps
when flag clicked
forever
if <key [right arrow v] pressed?> then
change x by (5)
end
end
Imagine you press and hold the right arrow for two whole seconds on each cat. Which one moves further? Which one moves more smoothly?
Reveal the answer
Cat B moves further and more smoothly. The hat in Cat A fires once on the first press — and then your operating system waits a bit, then "auto-repeats" the key at the OS's repeat rate (a few presses per second). Cat A jerks forward 10 steps, pauses, jerks again. Cat B's forever loop runs ~30 times a second and checks the boolean every frame — so it slides continuously at 5×30 = 150 steps per second while held. Cat B feels like a real game character. Cat A feels like a typewriter.
Same key, two completely different behaviours. Today's lesson is about knowing which one to reach for.
New Concept — the hat and the boolean 15 min
Scratch gives you two ways to react to a keyboard. They live in different palettes, look different, and behave differently. Pick the wrong one and your game feels broken.
The hat: when [key v] key pressed
Yellow, in the Events palette. It's a hat block — a script-starter, like when ⚑ clicked. Drop it at the top of a fresh script. It does two things:
when [space v] key pressed
say [Boing!] for (1) seconds
- Fires once per press. Hold the key down — Scratch (via your OS) will eventually auto-repeat it at the OS repeat rate, but it's choppy, not smooth.
- Starts its own script from scratch every time. If the previous run hasn't finished, you can end up with overlapping copies.
Use this when the press is the event: jump, fire, open the menu, advance the dialogue.
The boolean: <key [key v] pressed?>
Light blue, in the Sensing palette. It's a boolean reporter — a hexagonal block that answers true/false. It doesn't start anything on its own; you have to put it inside an if <> then inside a forever:
when flag clicked
forever
if <key [right arrow v] pressed?> then
change x by (5)
end
end
- True continuously while held. False the instant you release.
- Has to live inside a loop — bare, it gets read once and forgotten.
Use this when "while pressed" matters: walking, holding to charge a jump, holding to aim, hold-to-zoom, hold-to-talk.
The key dropdown
Both blocks share the same dropdown. Click the white triangle to see the list:
- any — true if any key on the keyboard is being pressed (or the hat fires on any keypress). Great for "press any key to start".
- space — the most common "do the thing" key.
- up arrow / down arrow / left arrow / right arrow — the classic four for movement.
- a, b, c, ... z — every letter on the keyboard.
- 0, 1, 2, ... 9 — every digit row.
Scratch's list is intentionally small — no Shift, no Ctrl, no Tab. That keeps lessons simple and forces game-makers to use real game keys (WASD, arrows, space).
Side by side
Here's the same idea — "move when right arrow is pressed" — built both ways. Try both in Scratch and feel the difference.
when [right arrow v] key pressed
change x by (10)
when flag clicked
forever
if <key [right arrow v] pressed?> then
change x by (5)
end
end
Worked Example — Priya's arrow-key cat 12 min
We're going to make the cat-pet do tricks on the keyboard — drive it with the arrows smoothly, then add a one-shot meow on space.
Step 1 — New project
Fresh Scratch project. Default cat. Drag it to roughly x: 0, y: 0.
Step 2 — Drop the hat and the forever
From Events: when ⚑ clicked. From Control: forever, snapped under.
Step 3 — Add the first if-then for right arrow
From Control: if <> then, inside the forever. From Sensing: drag <key [space v] pressed?> into the diamond. Click the dropdown on the boolean and change space to right arrow.
Step 4 — The action inside the if-then
From Motion: change x by (10). Drop it inside the if-then C. Click the flag, hold right arrow — cat slides right. Smooth!
Step 5 — Repeat for left, up, down
Right-click the if-then block and pick Duplicate. Drop the copy below the first one (still inside the forever). Change the boolean's dropdown to left arrow and the motion to change x by (-10). Duplicate again for up (change y by (10)) and once more for down (change y by (-10)).
Step 6 — Test the smooth four-direction movement
Click the flag. Hold combinations — left+up, right+down. The cat slides diagonally because both if-thens are true at the same time. Free, no extra code. This is the magic of the boolean approach.
Step 7 — Add a one-shot meow on space
Drag a separate hat — when [space v] key pressed — onto an empty area of the script. Snap a play sound (Meow v) until done under it. This is the hat-block job: one event, one sound.
Step 8 — Try them together
Click the flag. Drive the cat around with the arrows; tap space — meow. Tap space again — meow. Hold space — meow, pause, meow, pause (OS auto-repeat). The hat is doing exactly the right job for the meow, and the boolean is doing exactly the right job for the movement.
The full assembled stack
when flag clicked
forever
if <key [right arrow v] pressed?> then
change x by (10)
end
if <key [left arrow v] pressed?> then
change x by (-10)
end
if <key [up arrow v] pressed?> then
change y by (10)
end
if <key [down arrow v] pressed?> then
change y by (-10)
end
end
when [space v] key pressed
play sound (Meow v) until done
What you just built: the input layer of every top-down game ever made. Pick a key, gate an action with the boolean for "while held", or trigger it with the hat for "on press". You'll use this exact pattern in L02-41 (Catch the Kuih) and L02-42 (Simple Pong).
Try It Yourself — three keyboard drills 15 min
Goal: "Press any key to start." Build a sprite that says Hello! for one second the instant any key is pressed. Use the any option of the hat block.
when [any v] key pressed
say [Hello!] for (1) seconds
Think: Why is the hat better than the boolean here? (Hint: how often do you want the "Hello!" to fire?)
Goal: "Hold to grow." Make the cat slowly grow bigger while the up arrow is held, and slowly shrink while the down arrow is held. Use the boolean inside a forever.
when flag clicked
forever
if <key [up arrow v] pressed?> then
change size by (2)
end
if <key [down arrow v] pressed?> then
change size by (-2)
end
end
Think: If you used the hat when [up arrow v] key pressed → change size by (2) instead, what would holding the key feel like compared to the boolean version?
Goal: "Aljay's secret code." Make the cat say Unlocked! for two seconds only when the keys a, s, and d are all three held down at the same time. Hint: use <> and <> (you can nest these) to combine three booleans.
when flag clicked
forever
if <<key [a v] pressed?> and <<key [s v] pressed?> and <key [d v] pressed?>>> then
say [Unlocked!] for (2) seconds
end
end
Think: This is your first combined condition. The "say" only fires when all three answers are true at once. Three single-key if-thens would behave very differently — they'd fire whenever any of the three is pressed, not when all three are.
Mini-Challenge — Aisyah's jumpy character 5 min
"Why does the jump feel terrible?"
Aisyah is building a one-button game where pressing space makes her sprite "jump" (move up 50, then back down 50). She writes:
when flag clicked
forever
if <key [space v] pressed?> then
change y by (50)
wait (0.3) seconds
change y by (-50)
end
end
The problem: she picked the wrong block. Which one should she use, and why?
Reveal one valid solution
A jump is a one-shot event, not a "while held" action. Aisyah used the boolean-in-a-loop pattern, which fires once per frame the key is held — so a 0.5-second hold of space triggers maybe 15 overlapping jump scripts. The result is chaotic.
The fix is to use the hat block instead. The hat fires once per press, exactly the right behaviour for "jump":
when [space v] key pressed
change y by (50)
wait (0.3) seconds
change y by (-50)
Now one press = one jump. Hold space = the OS auto-repeat triggers another jump every few hundred milliseconds, but the script itself is single-shot per press — no chaos.
The lesson: the hat block matches "events that happen once" (jump, fire, dialogue advance). The boolean-in-a-loop matches "states that persist" (walking, holding a shield up, charging a meter). Pick the block whose natural rhythm matches the action.
Recap 3 min
You met the two keyboard listeners Scratch gives you, and learned that they aren't interchangeable. The yellow hat when [space v] key pressed fires once per press — perfect for jumps, shots, and dialogue advances. The blue boolean <key [space v] pressed?> is true while held — wrap it in if <> then inside forever for smooth movement and hold-to-charge actions. Both blocks share the same key dropdown: any, space, four arrows, every letter, every digit. Picking the right block for the job is the difference between a game that feels good and one that feels broken.
- Hat block
- A block that sits at the top of a script and starts it when something happens. when [space v] key pressed is one — it starts a fresh run of the script the moment a key is pressed.
- Boolean reporter
- A hexagonal block that answers true/false. <key [space v] pressed?> is true continuously while the key is held, false the rest of the time. Lives inside if-then or operators.
- Auto-repeat
- The operating system's behaviour of "fake re-pressing" a held key several times per second. The hat block sees these as separate presses; the boolean doesn't care because it reads the "is held" state every frame.
- One-shot event
- An action that should fire once when something happens. Jumps, shots, sound effects, menu opens. Match with the hat block.
- Continuous state
- An action that should happen while something is true. Walking, growing, gliding, draining a battery. Match with the boolean inside a forever loop.
Homework 2 min
Two-Mode Mover. Build a sprite with both styles of keyboard input in one project — so you can feel the difference.
- New project. Default cat at the centre.
- Script 1 (smooth movement): when ⚑ clicked → forever with four if-thens checking the four arrow-key booleans, each one changing x or y by 5.
- Script 2 (one-shot costume change): when [c v] key pressed → next costume. Each tap of c flips the cat between costume 1 and costume 2.
- Script 3 (one-shot meow on space): when [space v] key pressed → play sound (Meow v) until done.
Save as HW-L2-17-Two-Mode-Mover.sb3. Drive the cat around with arrows, mash c to flip costumes, hit space to meow. All three styles, one cat.
Bring back next class:
- The
.sb3file. - Your answer to: "Try rebuilding Script 2 using the boolean version — forever + if <key [c v] pressed?> then + next costume. Run it and hold the c key for one second. What happens to the costume? Why?"
Heads up for next class: SCR-L02-18 introduces a totally different kind of input — ask [What is your name?] and wait. Instead of one key at a time, the sprite asks the user to type a whole answer and reads it back. Conversations, quizzes, and choose-your-adventure stories all start here.