Game Development Reference
In-Depth Information
The generation of a KeyEvent object by the EventHandler object indicates that a
keystroke has occurred. A KeyEvent is often generated in a Scene Graph Node, such as
an editable text UI control, but in our case we are going to attach our event handling
above the Scene Graph Node hierarchy directly to the Scene object named scene , hop-
ing to avoid any Scene Graph processing overhead that would be incurred by attaching
event handling to any of the Node objects in the Scene Graph (in our case, this is cur-
rently the StackPane object named root ).
A KeyEvent object is generated whenever a key is pressed and held down, re-
leased , or typed (pressed and immediately released). Depending on the nature of this
key pressing action itself, your KeyEvent object is passed into either an
.onKeyPressed() , an .onKeyTyped() or an .onKeyReleased() method for further pro-
cessing inside the nested .handle() method, which is what will hold your game-specific
programming logic.
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, and are used to obtain character input for UI
controls such as text fields, and are used for business applications, such as calendars
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, the capital A can be 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 Alt-Key-with-Numeric-
keypad entry method, which was used “back in the day,” with DOS and held over into
Windows OSes.
It is important to note that no key-typed KeyEvent objects will be generated for
keys that do not generate any Unicode characters. This would include action keys or
modifier keys, although these do generate key-pressed and key-released KeyEvent ob-
jects, and could thus be used for game play! This would not represent a good user inter-
face design (or user experience design) approach, generally speaking, as these keys are
used to modify other key behavior.
The KeyEvent class has a character variable (I am tempted to call this a character
characteristic, but I won't) which will always contains a valid Unicode character for a
Search WWH ::




Custom Search