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 makesomething 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.

Print the lesson

Please study the material at each of the links below.

  1. A dog wags his tail continuously: The addSceneActivationListener makes a dog wag his tail continuously while other actions take place.
  2. Playing a background sound: To play background music use initializeEventListeners
    to play sound together with myFirstMethod.
  3. Click on the bear event: The bear says something when the user clicks on him.
  4. Click on several different objects: Each animal says something when clicked
  5. Drag and Drop: Using default model manipulation in event listeners lets the user drag and drop objects at run time.
  6. Collisions: Making something happen when objects collide.
  7. The keypress event: typing a letter to make a selection
  8. Object Mover and Key press events: Add object mover for the shark so that the user can move it with the arrow keys.
  9. A Timer Event: Using the timer event, the penguin says "Help!" at random intervals.
  10. Self Study Questions
  11. Discussion Questions

Active Learning

What events can you add?
Animate a fairy tale using all you have learned

Like us on Facebook:


OER: Open Educational Resource
Creative Commons License Author: Janet E. Joy; Publisher: Zebra0.com
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
Creative Commons Attribution-NonCommercial 4.0 International License
Last modified: May 17 2017 16:40:50.