Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 10 ■ User InterfaCe DesIgn InteraCtIvIty: event hanDlIng anD ImagIng effeCts


Alt-key-with-numeric-keypad entry method that was used “back in the day” with DOS and held over into
Windows OSs. It is important to note that no key-typed KeyEvent objects will be generated for computer
keyboard keys that do not generate Unicode (visible, printable) characters, such as the Shift, Control (Ctrl),
or Alternate (Alt) keys, commonly referred to as modifier keys.
The KeyEvent class has a character variable (I am tempted to call this a character characteristic, but I
won’t) that will always contain a valid Unicode character for a key-typed event or CHAR_UNDEFINED for a
key-pressed or key-released events. Character input is reported only for key-typed events, since key-pressed
and key-released events are not necessarily associated with character input. Therefore, your character
variable is guaranteed to be meaningful only for key-typed events.
For key-pressed and key-released KeyEvent objects, the code variable in the KeyEvent class will contain
your KeyEvent object’s keycode, defined using the KeyCode class you learned about earlier. For key-typed
events, this code variable always contains the constant KeyCode.UNDEFINED. So as you can see, key-
pressed and key-released are thus designed to be used differently than key-typed, and that is the reason
you will probably be using key-pressed and key-released events for game event handling. Key-pressed and
key-released events are low level and are generated whenever a given key is pressed or released. They are
the only way to “poll” the keys that do not generate character input. Your key being pressed or released is
indicated to the OS using the code variable, which contains a virtual KeyCode.


Adding Keyboard Event Handling: Using KeyEvents


I think that is enough background information for you to understand a basic example of how to implement
KeyEvent processing for pro Java games. It’s fairly straightforward, so I’m going to give you a quick overview
of how it is done here, since we are covering the KeyEvent class. This is also covered in my Beginning Java
8 Games Development book. The first thing that you would do is to add a line of code at the top of your
JavaFXGame class and declare four Boolean variables, named up, down, left, and right, using a single
compound declaration statement, shown here:


boolean up, down, left, right;


Since the default value of a boolean variable is false, this will signify a key that is not being pressed, that
is, a key that is currently released, or unused. Since this is also the default state for keys on a keyboard, this
would be the correct default value for this application. Since in Java, boolean variables default to false, you
do not have to explicitly initialize these variables.
I put an event handling foundation for a KeyEvent (key-pressed, in this instance) object in place
by using the .setOnKeyPressed() method call off of the Scene object named scene, which I have already
instantiated. Inside of this method call, I create the new EventHandler just like is done for the
ActionEvent. The code looks like this:


scene.setOnKeyPressed( new EventHandler() { a .handle() method will go in here } );


KeyEvent object processing just happens to be the perfect application for implementing the highly
efficient Java switch statement. You can add a case for each of the JavaFX KeyCode constants that you want
to process. These are contained inside of the KeyEvent object named event that is passed into this .handle()
method via its parameter area.

Free download pdf