28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9 ■ Controlling Your Action Figure: Implementing <strong>Java</strong> Event Handlers and Using Lambda Expressions<br />

The <strong>Java</strong> 8 and <strong>Java</strong>FX class hierarchy for the KeyEvent class jumps from the java.lang package to the java.util<br />

package to the javafx.event package to the javafx.scene.input package. The KeyEvent class hierarchy looks like the<br />

following:<br />

java.lang.Object<br />

> java.util.EventObject<br />

> javafx.event.Event<br />

> javafx.scene.input.InputEvent<br />

> javafx.scene.input.KeyEvent<br />

The generation of a KeyEvent object by the EventHandler object indicates that a keystroke has occurred. A<br />

KeyEvent is often generated in a Scene Graph Node, such as an editable text UI control, but in our case we are going<br />

to attach our event handling above the Scene Graph Node hierarchy directly to the Scene object named scene, hoping<br />

to avoid any Scene Graph processing overhead that would be incurred by attaching event handling to any of the Node<br />

objects in the Scene Graph (in our case, this is currently the StackPane object named root).<br />

A KeyEvent object is generated whenever a key is pressed and held down, released, or typed (pressed and<br />

immediately released). Depending on the nature of this key pressing action itself, your KeyEvent object is passed into<br />

either an .onKeyPressed(), an .onKeyTyped() or an .onKeyReleased() method for further processing inside the<br />

nested .handle() method, which is what will hold your game-specific programming logic.<br />

<strong>Games</strong> typically use key-pressed and key-released events, as users typically press and hold keys to move the<br />

actors in the game. Key-typed events on the other hand tend to be “higher-level” events and generally do not depend<br />

upon the OS platform or the keyboard layout. Typed key events (.onKeyTyped() method calls) will be generated when<br />

a Unicode character is entered, and are used to obtain character input for UI controls such as text fields, and are used<br />

for business applications, such as calendars and word processors, for instance.<br />

In a simple case, the key-typed event will be produced by using a single key press and its immediate release.<br />

Additionally, alternate characters can be produced using combinations of key press events, for instance, the capital A<br />

can be produced using a SHIFT key press and an ‘a’ key-type (press and immediate release).<br />

A key-release is not usually necessary to generate a key-typed KeyEvent object. It is important to notice that there<br />

are some fringe cases where a key-typed event is not generated until the key is released; a great example of this is the<br />

process of entering ASCII character code sequences, using that old-school Alt-Key-with-Numeric-keypad entry<br />

method, which was used “back in the day,” with DOS and held over into Windows OSes.<br />

It is important to note that no key-typed KeyEvent objects will be generated for keys that do not generate any<br />

Unicode characters. This would include action keys or modifier keys, although these do generate key-pressed and<br />

key-released KeyEvent objects, and could thus be used for game play! This would not represent a good user interface<br />

design (or user experience design) approach, generally speaking, as these keys are used to modify other key behavior.<br />

The KeyEvent class has a character variable (I am tempted to call this a character characteristic, but I won’t)<br />

which will always contains a valid Unicode character for a key-typed event or CHAR_UNDEFINED for a key-pressed<br />

or key-released event. Character input is only reported for key-typed events, since key-pressed and key-released<br />

events are not necessarily associated with character input. Therefore, the character variable is guaranteed to be<br />

meaningful only for key-typed events. In a sense, by not using key-typed events, we are saving both memory and CPU<br />

processing, by not having to process this Unicode character variable.<br />

For key-pressed and key-released KeyEvent objects, the code variable in the KeyEvent class will contain your<br />

KeyEvent object’s keycode, defined using the KeyCode class you learned about earlier. For key-typed events, this<br />

code variable always contains the constant KeyCode.UNDEFINED. So as you can see, key-pressed and key-released<br />

are thus designed to be used differently than key-typed, and that’s the reason we are using these for our game event<br />

handling.<br />

Key-pressed and key-released events are low-level, and depend upon platform or keyboard layout. They are<br />

generated whenever a given key is pressed or released, and are the only way to “poll” the keys that do not generate<br />

character input. The key being pressed or released is indicated by the code variable, which contains a virtual<br />

KeyCode.<br />

198

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!