CIUC- Creating Interactivity in Programs Lesson
Creating Interactivity in Programs
Writing an interactive program has one major difference from writing a program that is non-interactive. The difference is how the sequence of actions is controlled. In a non-interactive program, the sequence of actions is predetermined by the programmer. The programmer designs the program and then writes the code. Once the program is constructed and tested, then every time the program runs the same sequence of actions will occur. But, in an interactive program, the sequence of actions is determined at runtime.
An interactive program that changes its behavior in response to events is said to be an event-driven program.
The event is the action or occurrence detected by the program. In interactive programs, an event is usually triggered by:
• the user clicking the mouse, or pressing a key on the keyboard.
• a condition being met.
Once an event occurs, it triggers an event handler function or method. Event handlers are functions or methods that listen for particular events and respond to them. Once the event handler receives the event, it performs the code inside the event handler function. Event handlers are a central concept in event-driven programming.
Users add event handler functions to their programs to change program variables or the flow of the program.
There are several built in event handler functions in Processing that will allow you to write interactive programs. We will explore each in more detail in the next lessons. Below is a list of event handler functions that listen for events with the mouse and keyboard.
Mouse and keyboard events only work when a program has draw() function. Without draw(), the code is only run once and then stops listening for events.
[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION