Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 10 ■ User InterfaCe DesIgn InteraCtIvIty: event hanDlIng anD ImagIng effeCts

flag variables for each KeyCode mapping. The Boolean flags will provide an accurate view of what keys are
being pressed, or released, by a player in any millisecond.
These Boolean values can then be read and acted upon by using Java game programming logic in
your other game engine classes, which will then process these key events in real time so that your pro Java
gameplay works well and your user experience is good. Next, let’s take a look at the KeyEvent objects that
process these KeyCode objects.


The KeyEvent Class: Using KeyEvent Objects to Hold KeyCode Constants


Next, let’s take a closer look at the KeyEvent class. This class is designated public final KeyEvent, and it
extends the InputEvent superclass, which is used to create all of the input event subclasses that are in the
javafx.scene.input package. The KeyEvent class is set into motion using the EventHandler class and handles
KeyCode class constant values. This class’s hierarchy starts with the java.lang.Object master class and goes
through the java.util.EventObject event superclass to the javafx.event.Event class, which is used to create
the javafx.scene.input.InputEvent class that the KeyEvent class extends (subclasses). It is interesting to note
that we are spanning four different packages here.
The Java and JavaFX class hierarchy for the KeyEvent class jumps from the java.lang package to the java.
util package to the javafx.event package to the javafx.scene.input package. A KeyEvent hierarchy looks like
the following:


java.lang.Object



java.util.EventObject
javafx.event.Event
javafx.scene.input.InputEvent
javafx.scene.input.KeyEvent



The generation of a KeyEvent object by an EventHandler object indicates that a keystroke has occurred.
This KeyEvent is often generated using one of your Scene Graph Node objects such as an editable text UI
control; however, in the case of your game, you’re probably going to attach the key event handling above
the Scene Graph Node object hierarchy level directly to the Scene object named scene. This will serve to
minimize the Scene Graph pulse processing overhead by not attaching any KeyEvent handling to any of the
Node objects in your Scene Graph. In the case of your game, this is the root Group object containing the
uiLayout StackPane object and the gameBoard Group object.
A KeyEvent object is generated whenever a key is pressed and held down, released, or typed (pressed
and immediately released). Depending on the nature of this key pressing action itself, your KeyEvent object
is passed into an .onKeyPressed(), .onKeyTyped(), or .onKeyReleased() method for further processing
inside a .handle() method.
Games typically use key-pressed and key-released events, as users typically press and hold keys to move
the actors in the game. Key-typed events on the other hand tend to be “higher-level” events and generally
do not depend upon the OS platform or the keyboard layout. Typed key events (.onKeyTyped() method
calls) will be generated when a Unicode character is entered. They are used to obtain character input for
UI controls such as text fields and are used for business applications, such as calendars, calculators, e-mail
clients, and word processors, for instance.
In a simple case, the key-typed event will be produced by using a single key press and its immediate
release. Additionally, alternate characters can be produced using combinations of key press events. For
instance, a capital A is produced using a Shift key press and an “a” key-type (press and immediate release).
A key-release is not usually necessary to generate a key-typed KeyEvent object. It is important to notice
that there are some fringe cases where a key-typed event is not generated until the key is released; a great
example of this is the process of entering ASCII character code sequences using that old-school

Free download pdf