Events: Responding to the Mouse and Keyboard
Learn Alice in FREE step-by-step lessons.
Module 19: Events: Responding to the Mouse and Keyboard
The goal of this lesson is to learn to make your program more interactive
by allowing the user to control the action using the keyboard and the mouse.
Events are things that happen. In programming, some of the events that can occur are clicking or moving the mouse, typing a key on the keyboard, or other user interaction.
When these events occur, nothing will happen unless you tell the program to listen for these events and what to do when these events occur.
If we want to execute some statements when one of these events occurs, we have to tell Alice to "listen" for that event. We do this by adding a listener. We tell it what event to listen for, and what function to execute when that event occurs.
After adding an event listener, that event will trigger the execution of a procedure or function. Programmers refer to this as "Event Handling."
In this module you will learn to use event listeners to respond to the mouse and keyboard, add drag and drop and other interactive features.
In order to do this you will:
- Create code to implement event listeners for various events;
- Write code to determine which key on the keyboard was pressed;
- Write code to determine which object was clicked.
- Use control structures to specify which actions to take when an event occurs;
- Use addSceneActivationListener to create a continuous action while other actions take place;
- Play background music using initializeEventListeners;
- Write code the uses default model manipulation in event listeners to let the user drag and drop objects at run time;
- Use collision detection to make something happen when objects collide;
- Write code for the keypress event; for example typing a letter to make a selection;
- Add object mover for an object so that the user can move it with the arrow keys;
- Use the timer event to make an action occur at random intervals.