What thoughts come to mind when you hear the word “Events” (in coding)? The standard dictionary definition for it is an occurrence or a happening — and honestly, its definition in the scope of programming is not all that different.


What are events in coding?

If you want to get technical about it, an event is a change in the state of an object — one that is usually brought about as a result of some kind of user interaction, whether it’s pressing a key on the keyboard, pressing down on the left mouse, clicking a button, or tapping the screen of your phone. All of the objects in question — the keyboard, your mouse, a button, the screen of your phone — all have different states, right? Pressed and not pressed, clicked and not clicked, touched and untouched, and so forth.

But let’s not get too bogged down by the technicalities: an event is, essentially, something that happens. They can be user-generated events like some of the following:


Touch screen events Keyboard events
  • Detecting touch
  • Detecting gestures
  • Detecting the press of a certain key
Device events Mouse events
  • Rotation
  • Shaking
  • Tilting
  • Left/right mouse press up/down
  • Moving the mouse in a certain direction and/or a certain distance


There are, of course, events that aren’t generated by user inputs. Something like a web browser completely loading a web page, or a sensor receiving sensory input are both considered events.


What are events used for?

Events in coding are usually used as a means of triggering certain behaviours. We can use events to tell programs under what circumstance(s) a particular behaviour or set of behaviours should happen. They’re quite a useful concept in programming.

Have you ever heard of a program being referred to as event-driven? Event-driven programs are programs whose control flow is dictated by events. It might be a program that is always in a particular state until a certain event occurs. When the program detects the occurrence of said event, the program changes states so that it may appropriate handle said event.

Here’s an analogy to help better illustrate the point: it’s the school term, and your kid seems constantly bored. One day, you walk into their room and say, “We’re going to Disneyland tomorrow.” Your child, who was previously apathetic, suddenly changes their behaviour to better handle this event. Let’s break down this analogy into a control flow diagram to illustrate how the occurrence of an event — telling your child that you’re going to Disneyland tomorrow — dictates the change in their behaviour.


The (potential) control flow of a child.


Events in Scratch

You can find the Events category in Scratch under the Scripts tab. Let’s go through events in coding when using Scratch:


When flag clicked

The when flag clicked event lets us decide what should happen when we click the green flag button above the stage.


When key pressed

The when key pressed event lets us decide what should happen when we press a certain key on the keyboard. By default, it’s set to the space bar, but we can change this to a variety of other keys.

This block is actually one of the most common methods for getting sprites to move around the stage using the arrow keys:


The above code allows a sprite to move up with the up arrow key is pressed, down when the down arrow is pressed, and so on.


When sprite clicked

The when sprite clicked event lets us decide what happens when the user clicks on a sprite with the mouse.


When backdrop switches

The when background switches to X block acts as a pair with the switch background to X block, which you can find in the Looks category:

You can use the when background switches to X block to determine what will happen when the background is changed out using the block mentioned earlier.


When X is greater than X

The when X is greater than X block pairs with three blocks that you can find in the Sensing category: the loudness block, the timer block, and the video X on X block:

We’ll be addressing what these blocks do when we talk in future about Sensing blocks!


When I receive X

The when I receive X block pairs with the two broadcasting blocks beneath it:

Again, we’ll be addressing broadcasting in more detail in a later view!


So, let’s quickly review what we’ve learned:

  • An event is a change in the state of an object. Usually this change in states is brought about by user behaviour.
  • Things like mouse clicks, key/button presses and screen touching are all examples of events.
  • Not all events are user-created, however. A web browser fully loading a web page is also considered an event.
  • In programming, events are used as a means of dictating the control flow of a program. In other words, events are used as a means of triggering certain behaviours in your program.
  • You can find a range of events in Scratch in the Events category under the Scripts tab.

Now have a go in your own projects and use “events” in coding.

Happy Scratching!


Check out our free teacher download. Learn to code with Scratch in 30 minutes.

If you’re interested to find out more