LightMySky

Events: Programs That React to What the User Does

A program does not have to run from top to bottom. A handler sits waiting for a click, a key press or a collision, and runs only when that thing happens.

No account needed. Progress saves in this browser.

What a learner can do afterwards

  • Attach one handler to a key press and another to a click in the same program, and say which runs when
  • Explain why an event-driven program looks like it is doing nothing until the user acts
  • Fix a program where the handler was attached to the wrong object or the wrong event

1 · Read

Some programs do not run from top to bottom. A handler is a small set of instructions that sits waiting for one event, like a click or a key press. It runs only when that event happens.

Try it together

Imagine a game where the left arrow moves your sprite and a click changes its colour. Pressing the key runs the key handler, and clicking runs the click handler. Tapping a button on a page can run code that changes its colour, which tells you it was clicked.

That is why an event program looks like it is doing nothing. It is not stuck, it is listening. Nothing moves until you act.

Good to know

If the wrong thing happens, check two things. First, is the handler attached to the right object. Second, is it listening for the right event. A handler can also test whether a text field holds a name before it changes a colour. A click handler on the wrong button never fires when you click yours.

Handlers wait for their event, then run, so nothing happens until you click or press.

2 · Watch

Take it off screen

Print a worksheetA4 with an answer key page for grown-ups. No screen, no internet.

Where it sits

Where this leads

Jobs that lean on this skill. Follow one to see everything it is built on.

Then practise

8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.

Spotted a problem on this page? Tell us
Events: Programs That React to What the User Does · Computing, ages 12 to 13 · LightMySky