Events and triggers in a program
Computer Science · Key Stage 2 · The School
Waiting for something to happen
An EVENT is something that happens while a program runs: a key is pressed, a button is clicked, two sprites touch. A program can WAIT for an event and then react to it, instead of running straight through from top to bottom. Nothing in the program has to check for it over and over by hand.
When this, do that
Most event rules follow the same shape: WHEN the event happens, DO this action. "When the green flag is clicked, start the game." "When the space key is pressed, jump." The event is the trigger; the action is the response. Learning to spot this shape makes reading somebody else's program far easier. Once you know the shape, most rules become quick to read at a glance.
More than one event at once
A single program can wait for several different events, each with its own action. A game might have one rule for the space key, another for touching an enemy, and another for reaching the edge of the screen. Each runs only when its own event happens. None of the rules gets in the way of the others. Adding a new rule later does not require touching any of the existing ones.
A doorbell as an event rule
WHEN the button is pressed, DO ring the bell. Nothing happens the rest of the time. The doorbell is not constantly ringing or constantly checking in a way you can see — it is simply waiting for its one event, ready to react the instant it happens. A smoke alarm works the very same way, waiting silently for smoke. Both sit quietly for most of their working life.