🖱️ Events: When Something Happens
3rd Grade 💻 Coding
The programs we’ve written so far ran from start to finish without asking anyone anything. But games and apps wait for us: a click, a key press, a tap on the screen. Each of those is called an event, and the program responds to it. In this lesson we learn how to write code that reacts, and we meet two important words: input and output.
What is an event?
An event is something that happens while a program is running: the user clicks a button, presses a key, moves the mouse or touches the screen — or a certain amount of time passes, or two characters bump into each other. An event-driven program doesn’t just run from top to bottom. It waits, and every time an event happens, it runs the piece of code that matches it. That piece of code is called an “event handler.”
Here’s an example from real life: a doorbell. It doesn’t ring all the time, and it doesn’t check every second, “Is anyone here?” — it waits. When someone presses it (the event), it rings (the response). An elevator, a vending machine and a TV remote all work the same way: they “listen” for button presses and respond to each one differently.
Events in Scratch
Scratch has a whole category of yellow blocks called “Events.” These blocks have a special shape with a rounded “hat” on top, because they always sit at the top of a stack of blocks — they’re what starts it. The most famous one is “when green flag clicked”: everything snapped underneath it starts running when someone clicks the green flag above the stage.
There are lots more: “when space key pressed” is perfect for jumping in a game. “When this sprite clicked” works great for a button. And there’s an event that sprites send to each other: “broadcast message” and “when I receive.” For example, when the player wins, their sprite broadcasts “victory,” and another sprite that receives the message shows fireworks. That’s how sprites “talk” to each other without knowing what’s going on inside each one.
Input and output
Information that comes into a program from outside is called input: a key press, a mouse click, talking into a microphone or answering a question. Information the program sends out is called output: text on the screen, a sound, a picture or a vibration. Almost every program does three things: it takes in input, processes it and gives back output.
In Python you can ask for input with the input function: name = input("What is your name? ") shows the question, waits for the user to type and press Enter, and saves whatever was typed in the variable name. Then print("Hello " + name) sends out output. Watch out: input always gives back text. If you asked for someone’s age and need to do math with it, you turn it into a number with int, like age = int(input("How old are you? ")).
Events on websites and in apps
Every website you visit is full of events: clicking a link, typing in a search box, scrolling down the page. In JavaScript, the language that runs most websites, you can write that when a click event happens on a button, a certain function should run. That’s how an “Add to list” button really adds something, and how a search box shows suggestions while you type.
When you design an event-driven program, it’s smart to ask: What things could happen? What should happen in response to each one? And what if two events happen almost at the same time — for example, someone double-clicks the same button really fast? Good programmers think about those cases too, so the program doesn’t accidentally do something twice.
Key facts
- An event is something that happens while a program is running, like a click or a key press.
- An event handler is a piece of code that runs when the event happens.
- In Scratch, the Events blocks are yellow, and they always sit at the top of a stack of blocks.
- Input is information coming into a program; output is information it sends out.
- In Python, the input function always gives back text.
Quick quiz
What is an event in programming?
Which Scratch block starts a program when you click the flag?
What is input?
Which of these is output?
What does input do in Python?
How do sprites in Scratch “talk” to each other?
Which device works like an event-driven program?
Which language runs most of the events on websites?
💭 Think and talk about it
- Find three devices at home that respond to events. What’s the event and what’s the response for each one?
- What events would you need for a soccer game on the computer?
- What could go wrong if someone clicks a “Send” button twice really fast? How would you prevent it?
Questions kids ask
What’s the difference between an event and a loop?
A loop repeats instructions over and over by itself. An event runs instructions only when something happens from outside. In practice they work together: behind the scenes, there’s a loop that keeps checking whether an event has happened.
Why does input in Python give back text even when you type a number?
Because the keyboard sends characters — even the digits 1 and 2 are characters. The program can’t know whether you meant a number or text, so you convert it yourself: int(...) turns it into a whole number.
What is “broadcast message” in Scratch?
A block that lets one sprite send out a message with a name, and every sprite that has a “when I receive” block with that same name starts running. It’s a way to coordinate sprites — for example, to switch the backdrop when a level ends.