Board Events

The onEvent() block is used to set up event handlers that can watch for certain events to occur in your app, such as clicking on a button, selecting an item from a dropdown, or moving the mouse. The Circuit Playground has a similar block called onBoardEvent() to watch for events that occur on the board. Similar to onEvent(), you need to specify what the handler should be watching, what event to watch for, and how to respond to that event.

onBoardEvent

It's important to understand in Event-Driven programming that:

  • Users trigger events - Events occur when users interact with inputs on the board, such the buttons, the switch, and others.
  • Events trigger code - When an event occurs or "fires" it can be used to trigger a particular function.

The Maker Toolkit onBoardEvent() block is a type of event-handling function called an Event Listener. It wraps all of the setup up you need to do event handling into one command that has several parts. Here is an example with everything labeled:

  1. Code to execute when callback function is triggered when the user clicks the left button, buttonL. In this case, write the word "click" to the console. Notice that we're using the variable buttonL (no quotation marks). This is different from onEvent, which uses a string to specify the ID of a design element.

  2. Notice that this function, unlike most others we've seen, does not have a name. It is also called a callback function which is an odd term. A callback function is only different from a normal function in that you don't call it directly from your code. Instead a callback function is called by the system at the time the specified event occurs. Callback functions are a common pattern in a lot of event-driven programming.

Found a bug in the documentation? Let us know at documentation@code.org