💻 Computer Science · Topic 35 of 80

Event-Driven Programming

Programs respond to clicks, keys and messages.

Interactive labFollow the idea step by step

About this topic

In event-driven programming, a program waits for events, such as a mouse click, key press, timer or message arrival, and runs a piece of code called an event handler or callback in response. Instead of following a fixed path from start to finish, control is guided by what the user or system does.

A main loop, sometimes called the event loop, continually checks for events and sends each to the matching handler. In a web page, you might attach a click handler to a button so that clicking it changes the text. In Scratch, the when green flag clicked and when key pressed blocks are event handlers.

Graphical apps, games, phone apps and servers all use this style because users act unpredictably. A game does not know when the player will press jump, so it simply reacts when the key event occurs. A common problem is making a handler take too long, which freezes the interface because other events must wait. Another mistake is forgetting to register the handler, so nothing happens. Keep handlers short and test with different sequences of events, including clicking things in unexpected order.

Key ideas

  1. Event listeners
  2. Callbacks
  3. Used in GUIs and games

Quick check

Use cases: where Event-Driven Programming helps

Computing ideas run phones, search engines, games and the wider internet.

Frequently asked questions

What is Event-Driven Programming?

Programs respond to clicks, keys and messages.

What will I learn in Event-Driven Programming?

Event listeners. Callbacks. Used in GUIs and games.

Is Event-Driven Programming part of Computer Science?

Yes. It is one of 80 Computer Science topics on LearnViz HighSchoolEdu, with 3 key ideas, a live animation and a quick check.

Does the Event-Driven Programming lab work on a phone or tablet?

Yes. The animation resizes to fit your screen and supports touch, so you can drag, tap and slide with your finger.

How should I study Event-Driven Programming?

Read the summary and key ideas first, play with the interactive lab to see the idea in motion, then answer the quick check. If you get a question wrong, go back to the key idea it tests.

Do I need an account, and is my progress saved?

No account is needed. Marking a topic as learned is saved in your own browser, so it stays on that device and is not sent to a server.

More in Computer Science