An event handler is code that is associated with a particular event, such as "button A pressed". You create (or register) the association between an event and an event handler by calling a function named "on <event>". After registering an event handler with an event, then whenever that event occurs, the event handler code executes.
### Registering an event handler
Functions named "on <event>" create an association between an event and the event handler code. For example, the following code registers the event handler (the code between the `do` and `end` keywords) with the event of a press of button A:
After this code executes, then whenever button A is pressed in the future, the string "hello" will be printed.
### Event handlers are active for the entire program execution
Once you have registered an event handler for an event, like above, that event handler is active for the rest of the program execution. If you want to stop the string "hello" from printing each time button A is pressed then you need to arrange for the following code to execute:
The answer is that whenever button A is pressed, the string "goodbye" will be printed. If you want both the strings "hello" and "goodbye" to be printed, you need to write the code like this:
To learn more about how the BBC micro:bit queues up and schedules event handlers, see [the BBC micro:bit - a reactive system](/microbit/device/reactive)
### see also
[on button pressed](/microbit/reference/input/on-button-pressed), [on logo up](/microbit/functions/on-logo-up), [on logo down](/microbit/functions/on-logo-down), [on screen up](/microbit/functions/on-screen-up), [on screen down](/microbit/functions/on-screen-down), [on shake](/microbit/reference/input/on-gesture), [on pin pressed](/microbit/reference/input/on-pin-pressed)