Handling Input Events (Creating a User Interface in JavaFX)

So far we’ve shown you a couple of examples of event handling. For example, we used the onAction event handler to execute code when a button is clicked. We also used the onCloseRequest event handler of the Stage class to execute code when the Stage has been requested externally to close. In this section, we explore more of the event handlers available in JavaFX.

Surveying Mouse and Keyboard Events and Handlers

Most of the events that occur in JavaFX programs are related to the user manipulating input devices such as a mouse and keyboard. To see the available event handlers and their associated event objects, we take yet another look at the JavaFX API documentation. First, navigate to the javafx.scene.Node class and look for the properties that begin with the letters "on". These properties represent the event handlers common to all nodes in JavaFX. Here is a list of these event handlers in the JavaFX 2.0 API:

• Key event handlers: onKeyPressed, onKeyReleased, onKeyTyped

• Mouse event handlers: onMouseClicked, onMouseDragged, onMouseEntered, onMouseExited, onMouseMoved, onMousePressed, onMouseReleased

• Drag and drop handlers: onDragDetected, onDragDone, onDragDropped, onDragEntered, onDragExited, onDragOver

Each of these is a property that defines a method to be called when particular input events occur. In the case of the key event handlers, as shown in the JavaFX API docs, the method’s parameter is a javafx.scene.input.KeyEvent instance. The method’s parameter for the mouse event handlers is a javafx.scene.input.MouseEvent.


Understanding the KeyEvent Class

Take a look at the JavaFX API docs for the KeyEvent class, and you’ll see that it contains several methods, a commonly used one being getCode(). The getCode() method returns a KeyCode instance representing the key that caused the event when pressed. Looking at the javafx.scene.input.KeyCode class in the JavaFX API docs reveals that a multitude of constants exist that represent keys on an international set of keyboards. Another way to find out what key was pressed is to call the getCharacter() method, which returns a string that represents the unicode character associated with the key pressed.

The KeyEvent class also enables you to see whether the Alt, Ctrl, Meta, and/or Shift keys were down at the time of the event by calling the isAltDown(), isControlDown(), isMetaDown(), or isShiftDown() methods, respectively.

Understanding the MouseEvent Class

Take a look at the MouseEvent class in the JavaFX API docs, and you see that significantly more methods are available than in KeyEvent. Like KeyEvent, MouseEvent has the isAltDown(), isControlDown(), isMetaDown(), and isShiftDown() methods, as well as the source field, which is a reference to the object in which the event originated. In addition, it has several methods that pinpoint various co-ordinate spaces where the mouse event occurred, all expressed in pixels:

• getX() and getY() return the horizontal and vertical position of the mouse event, relative to the origin of the node in which the mouse event occurred.

• getSceneX() and getSceneY() return the horizontal and vertical position of the mouse event, relative to the Scene.

• getScreenX() and getScreenY() return the horizontal and vertical position of the mouse event, relative to the screen.

Here are a few other commonly useful methods:

• isDragDetect() returns true if a drag event is detected.

• getButton(), isPrimaryButtonDown(), isSecondaryButtonDown(), isMiddleButtonDown(), and getClickCount() contain information about what button was clicked, and how many times it was clicked.

A little later in this topic you get some experience with creating key and mouse event handlers in the ZenPong example program. To continue preparing you for the ZenPong example, we now give you a look at how you can animate the nodes that you put in your scene.

Next post:

Previous post: