Level 5

Is That Key Down?

Level 5 · Chapter 3

What this lesson follows

CSTA K–12 Computer Science Standards (2017)

Algorithms & Programming · Grades 6–8

Combine loops and conditions, even nested ones.

Code 2-AP-12Read the official text

Also practised: 2-AP-17

Common Core State Standards for Mathematics

Expressions & Equations · Grade 6

Show how one amount changes with another.

Code 6.EE.C.9Read the official text

The short lines are our own words. We matched this lesson to these standards ourselves; the standards' owners have not reviewed or endorsed this course.

Common Core State Standards © 2010 National Governors Association Center for Best Practices and Council of Chief State School Officers. All rights reserved.

Today you will use key-pressed as a live Boolean condition, not just an event trigger.

1👋 Warm-Up

For teachers · goals · guide
Lesson Outcomes · for teachersCSTA 2
✦ CSTA 2-AP-12 · CSTA 2-AP-17 · ISTE 1.5.c · Standards
🎯 I canuse key-pressed as a live Boolean condition, not just an event trigger
📝 New words7 words
⏱ Lesson time~40 min
Teacher Guide

📋 Before class

⚠️ Common misconceptions

  • Students often believe when key pressed and if do the same thing. The critical difference: when key pressed fires once per keydown event, while if inside forever is checked every frame — enabling smooth, held-key movement.
  • Students may think two keys cannot be true simultaneously. They can — polling inside one forever loop detects both at the same frame.

🎯 Differentiation

🐢 Struggling: Build the two versions side by side (event vs. polling) and have students hold the key for 3 seconds. The difference in smoothness is immediately felt.

🚀 Advanced: Challenge students to add acceleration: instead of a fixed step size, use a velocity variable that increases while a key is held and decreases when released.

💡 During the lesson

  • Warm-UpAsk: "When you hold a movement key in a game, does the character move smoothly or in jerks?" Choppy = the game reacts only to taps. Smooth = the game keeps checking while you hold. Students have felt both without knowing the cause. The block that keeps checking is a condition ❓ — a Boolean ✅ test that is only ever true or false.
  • Try ItRun Approach A live — hold the key and observe the stutter. Then run Approach B — hold the key and feel the difference. Ask: "Why does Approach A stutter?" (OS key-repeat delay.) "Why does B feel smooth?" (Checked every frame, not waiting for OS events.)
    Diagonal movement (holding two keys) is a key insight. Both if blocks run in the same frame, so both x and y change simultaneously. Ask: "How many if blocks fire in one frame if you hold right and up?" (Two — both conditions are true.)
  • PlanAsk students: "Why use 4 separate if blocks instead of if-else?" (If-else would prevent diagonal movement — only one branch runs per frame. Separate ifs allow multiple to run together.)
  • BuildStep 7 introduces nested if blocks for a modifier key. This previews the concept of key combinations. In step 10 every student abstracts the step size into a reusable variable.