Game Development Reference
In-Depth Information
Figure 9-9
.
Add a .setOnKeyPressed() function call off a scene object and create a new EventHandler<KeyEvent>
object
As you can see at the bottom of
Figure 9-9
, I put the foundation for my KeyEvent
(pressed) handling using the .setOnKeyPressed() method call off of the Scene object
named scene, which I have instantiated in the previous line of code. Inside of this
method call I create a new EventHandler<KeyEvent> just like we did for our action
events. The code, which as you can see has an error message attached to it until you
import a
KeyEvent
class, looks like this:
scene
.setOnKeyPressed
(new EventHandler<KeyEvent>() { your
.handle() method will go in here });
Use an
Alt-Enter
work process to select an
import javafx.scene.input.KeyEvent
option seen in
Figure 9-9
, to remove this error message. Next, let's take a look at the
.handle() method we need to write to process the KeyEvent.