Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Computational Thinking · Grade 6-8

Event

A detectable action or occurrence in a program — a user click, key press, mouse movement, or timer expiry — that the program can respond to by running a predefined handler.


Quick Answer

A detectable action or occurrence in a program — a user click, key press, mouse movement, or timer expiry — that the program can respond to by running a predefined handler. Event-driven programs do not run straight from top to bottom; they set up handlers and then wait. Use events when a program must react to something whose timing it does not control, such as a button tap, an arrow key in a game, a message arriving or a timer reaching zero. Each time the event happens, its handler runs.

Why This Matters

Every app, game and web page you use is built on events: nothing happens until you tap, type or scroll, and then the right piece of code runs. Thinking in events lets you build programs that respond to people in whatever order they act. Forget to attach a handler and the button does nothing; write a slow handler and the whole app freezes until it finishes.

Intuitive Explanation

A recipe-style program runs its lines in order and finishes: read two numbers, add them, print the sum. A game cannot work that way. It has no idea when the player will press the space bar, or whether they will press it at all. So instead of a fixed order, the program says in advance: when the space bar is pressed, run jump. That pairing of an event (space bar pressed) with a handler (jump) is set up once, and then the program waits.

Each event carries a little information: a click event says where the pointer was, and a key event says which key. In Scratch, the block 'when this sprite clicked' is an event and the blocks under it are the handler. In JavaScript, button.addEventListener('click', showMessage) attaches the handler showMessage to the button's click event. Click three times and showMessage runs three times; do not click and it never runs.

Not everything that changes is an event. A loop that checks the score 60 times a second is the program asking a question over and over, not reacting to a signal. A calculation that runs line by line after the program starts is plain sequence. What marks an event is that something outside the program's own steps, a person, the clock or the network, decides when the code runs.

Think of it like this. A doorbell works like an event: nobody checks the porch every minute; when a visitor presses the button, the chime (the handler) sounds. Wire the button to nothing and the press still happens but nobody hears it, just like an event with no handler attached. One difference: a single program event can trigger several handlers.

Core idea. Event-driven programs wait for things to happen rather than following a fixed sequence.


When to Use

Think in events when the program must react to something whose timing it does not control: a click, a key press, a touch, a timer finishing, data arriving. The word when ('when the button is clicked, show the menu') usually marks an event. If the question is about the code that runs in response, its steps and what it changes, that is the Event Handler. If it is about where buttons sit on the screen, that is User Interface.

Tip. Ask: Does the program have to react to something whose timing it does not control, such as a click or a timer?

How to Recognize It

Look at what starts the action: an event is the happening itself, which the program cannot schedule.

  1. Is something happening outside the code, such as a tap, a key press, a message arriving or a countdown ending?

    Yes points to an event. If the code itself decides when each line runs, top to bottom, you are looking at Sequence instead.

  2. Are you naming what happened, rather than writing the code that responds to it?

    Yes keeps you at the event. If the question is about the function that runs afterwards, its body or how it is attached, that is an Event Handler.

  3. Could it happen at any moment, or in any order, while the program waits?

    Yes is typical of events. If the program stops at a set point and asks the user to type a value, such as input() in Python, that is Input/Output.

  4. Is the question about the action, not the on-screen button or menu where it happens?

    Yes means event. If it asks how the buttons, menus and layout look or are arranged, it is about the User Interface.

Event vs Event Handler vs Input/Output vs User Interface

All four show up in any app with buttons, which is why they blur. Ask what you are pointing at: the happening, the code that answers it, the data going in or out, or the controls on the screen.

Event

Meaning
An event is something that happens while a program runs, such as a click, a key press or a timer reaching zero.
Key test
It is a happening at a moment in time, not code and not a screen object.
Example
The moment a player presses the space bar during a game.

Event Handler

Meaning
An event handler is the function the program runs automatically each time a particular event occurs.
Key test
It is code you write and attach; the event is what calls it.
Example
A function that makes the character jump, attached to the space bar's key-down event.

Input/Output

Meaning
Input/Output is the data a program takes in from outside and the results it sends back out.
Key test
It is about the data itself, like the letter typed, not the moment it arrived.
Example
The program reads the name 'Maya' from the keyboard and prints 'Hello, Maya'.

User Interface

Meaning
A user interface is the set of screens, buttons, menus and other controls a person uses to work a program.
Key test
It is something you can see or touch on screen, where events happen.
Example
A music app's play button, volume slider and song list.

Formula & Notation

Worked Examples

Example 1 — Trace a click counter

Easy

A web page runs: count = 0, then button.addEventListener('click', addOne), where addOne sets count = count + 1 and shows count on the screen. The page loads, the user clicks the button 3 times, then waits a minute. What does the screen show after each click, and when does addOne run?
Show the steps and the answer
  1. On load, set count = 0 and attach addOne to the button's click event. Nothing else runs.

    Attaching a handler does not call it; the program only records 'when clicked, run addOne'.

  2. Click 1: addOne runs, count = 0 + 1 = 1, and the screen shows 1. Click 2: count = 2. Click 3: count = 3.

    The handler runs once per event, so three clicks mean three runs.

  3. During the minute of waiting, addOne does not run and count stays 3.

    No event, no handler run. The program sits idle instead of looping through addOne.

Answer: The screen shows 1, 2 and 3 after the three clicks. addOne runs exactly three times, once per click, and never while the user waits.

Takeaway: An event-driven program sets up its handlers and then waits; each event runs its handler once.

Example 2 — Same jump, loop or event?

Standard

Two versions of a game make a character jump. Version A: repeat forever, 30 times a second: check whether the space key is down; if it is, jump. Version B: when the space key is pressed, run jump. In one minute the player presses space 5 times. Which version uses an event, and how often does each version's code run?
Show the steps and the answer
  1. Version A: the check runs 30 × 60 = 1,800 times in the minute.

    A loop runs on its own schedule whether or not anything happened, so this is the program asking, not reacting.

  2. Only the checks that happen while space is held down lead to a jump; most of the 1,800 find nothing.

    Checking over and over wastes work when the action is rare, and a tap that starts and ends between two checks would be missed completely.

  3. Version B: jump runs 5 times, once per press.

    The key press is an event. The system notices it and calls the handler, so code runs only when something actually happens.

Answer: Version B uses an event; its handler runs 5 times. Version A is a loop that checks 1,800 times a minute, so it is not event-driven, even though the character also jumps.

Takeaway: The same behavior on screen can come from a loop or an event. If the code runs only when the action happens, it is event-driven.

Example 3 — Find the mistake: a handler never attached

Application

Lena builds a quiz page. She writes a function checkAnswer() that compares the typed answer with 42 and shows 'Correct' or 'Try again', and she adds a Submit button. Clicking Submit does nothing, so she writes: "checkAnswer must be broken." But when she calls checkAnswer() directly in a test, it shows the right message. Find the mistake and correct it.
Show the steps and the answer
  1. Test the handler on its own: calling checkAnswer() directly shows the right message.

    The function works, so the bug is not inside it, and Lena's guess is wrong.

  2. Look for the link between the button and the function: there is no addEventListener line for the Submit button.

    Defining a function does not connect it to anything. The click event fires, but no handler is attached to it.

  3. Attach it: submitButton.addEventListener('click', checkAnswer).

    Pass the name without parentheses. Writing checkAnswer() there would run it once when the page loads and attach its result instead of the function.

  4. Click Submit with 42 typed: 'Correct' appears. With 41 typed: 'Try again'.

    Trying both outcomes shows the click event now reaches the handler.

Answer: Register the handler with submitButton.addEventListener('click', checkAnswer); every click then runs checkAnswer.

Takeaway: A handler does nothing until it is attached to an event. When a click does nothing, check the attachment before the function.

Where You'll See It

  • Tapping a button in an app

    Your tap is an event; the app's button has an event handler that runs the moment the tap is detected, like sending a message or playing a sound.

  • Pressing arrow keys in a video game

    Each key press fires a key-down event, and the game's handler moves your character; nothing happens until that event occurs, so the program waits and reacts.

  • A microwave timer beeping

    When the countdown reaches zero, a timer-expiry event fires and triggers the beep handler, an example of an event that comes from time rather than a user.

Common Mistakes

  1. The slip: Writing the handler function is enough to make a click do something.

    Why it's tempting: The handler clearly contains the action, so it feels like the work is done.

    Instead: You must also register (attach) the handler to the event source. If it isn't registered, the event fires but no code runs.

  2. The slip: Events always happen in the order I planned.

    Why it's tempting: When you test, you click things in a tidy sequence, so order seems guaranteed.

    Instead: Users can trigger events in any order, including ones you didn't expect; event-driven code must handle events arriving in any sequence.

  3. The slip: It's fine for an event handler to do a lot of slow work.

    Why it's tempting: It's just one function, so it seems harmless to put everything in it.

    Instead: A slow handler blocks the program from responding to other events, making the app feel frozen; keep handlers fast or do long work separately.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. A counter starts at 0. Clicking the button adds 1; pressing R sets it back to 0. The user clicks, clicks, presses R, then clicks three more times. What is the counter?

    Hint: Handle the events one at a time, in the order they happen.

  2. In a game, each spacebar press makes the player jump, and a timer event every 0.5 s moves an enemy 10 pixels left. The enemy starts at x = 300. After 3 s with two spacebar presses, where is the enemy, and how many jumps happened?

    Hint: Count how many timer events fit into 3 seconds. Key presses and timer ticks are separate events.

  3. A button has three handlers: mouse enter turns it yellow, click turns it green, mouse leave turns it grey. The user moves onto the button, clicks, then moves away. What colour is it at the end?

    Hint: List the three events in the order they fire.

  4. List every event in this app description: 'When the page loads, show a welcome message. When the user types in the search box, filter the list. Every 60 seconds, refresh the news.'

    Hint: Look for the trigger in each sentence, not the action that follows it.

  5. A program calls drawStar() on line 10 of its main code. It also runs drawStar() whenever the user taps the screen. Which run is a response to an event, and which is a plain function call?

    Hint: Ask whether the program is waiting for something to happen.

  6. A student writes function addPoint() { score = score + 1 }, but clicking the button does nothing. They say: 'The click event must be broken.' What is the real problem?

    Hint: Writing a function is one step. What tells the button to use it?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

If I click a button twice really fast, does the handler run twice?

Usually yes. Each click is a separate event, and the program keeps events in a line called a queue and handles them one at a time, in the order they arrived. That is why double-clicking a 'Buy' button can place two orders. Programmers stop this by disabling the button after the first click, or by having the handler ignore repeats that arrive within a short time.

Is checking 'is the key pressed?' inside a loop the same as using an event?

Not quite. Checking again and again in a loop is called polling: the program keeps asking 'has it happened yet?' many times a second, even when nothing happens. With events, the program tells the computer which handler to run and does nothing until the event arrives. Many simple games poll the keyboard once per frame, which works, but events waste less work and catch short taps that polling between frames can miss.

Can an event happen without a person doing anything?

Yes. Sensors and networks create events too. A smart thermostat gets an event when its temperature reading crosses 20 degrees Celsius, a chat app gets one when a message arrives from the server, and a web page gets a 'load' event when it finishes downloading. In each case the program did not choose the moment; something outside the code did.

Does Scratch use events?

Yes, Scratch is built around them. Every yellow 'when' block, such as 'when green flag clicked', 'when this sprite clicked' or 'when space key pressed', is an event, and the blocks stacked under it are the handler. A script with no 'when' block on top never runs on its own, which is the Scratch version of a handler that was never attached.

Learning Path

← Before

Function
Event

You are here

You need Function first: a named block of code you can call. An event adds a trigger that calls a function for you, at a moment chosen by a user or the clock. Next, Event Handler looks closely at the function that responds (what it receives and what it changes), and User Interface designs the buttons and controls that produce events.

See Also