Java Reference
In-Depth Information
Drag-and-drop handlers
:onDragDetected , onDragDone , onDragDropped , onDragEntered , onDragExited , onDragOver
Touch handlers:
onTouchMoved , onTouchPressed , onTouchReleased , onTouchStationary
Gesture handlers:
onRotate , onRotationFinished , onRotationStarted , onScroll ,
onScrollStarted , onScrollFinished , onSwipeLeft , onSwipeRight , onSwipeUp , onSwipeDown ,
onZoom , onZoomStarted , onZoomFinished
Each of these is a property that defines a method to be called when particular input events occur. In the case of
the key event handlers, as shown in the JavaFX API docs, the method's parameter is a javafx.scene.input.KeyEvent
instance. The method's parameter for the mouse event handlers is a javafx.scene.input.MouseEvent . Touch
handlers consume a javafx.scene.input.TouchEvent instance, and when a gesture event occurs, the method's
parameter for the handle event is an instance of javax.scene.input.GestureInput .
Understanding the KeyEvent Class
Take a look at the JavaFX API docs for the KeyEvent class, and you'll see that it contains several methods, a commonly
used one being getCode() . The getCode() method returns a KeyCode instance representing the key that caused
the event when pressed. Looking at the javafx.scene.input.KeyCode class in the JavaFX API docs reveals that a
multitude of constants exist that represent keys on an international set of keyboards. Another way to find out what
key was pressed is to call the getCharacter() method, which returns a string that represents the unicode character
associated with the key pressed.
The KeyEvent class also enables you to see whether the Alt, Ctrl, Meta, and/or Shift keys were down at the time of
the event by calling the isAltDown() , isControlDown() , isMetaDown() , or isShiftDown() methods, respectively.
Understanding the MouseEvent Class
Take a look at the MouseEvent class in the JavaFX API docs, and you see that significantly more methods are
available than in KeyEvent . Like KeyEvent , MouseEvent has the isAltDown() , isControlDown() , isMetaDown() , and
isShiftDown() methods, as well as the source field, which is a reference to the object in which the event originated.
In addition, it has several methods that pinpoint various coordinate spaces where the mouse event occurred, all
expressed in pixels:
getX() and getY() return the horizontal and vertical position of the mouse event, relative to
the origin of the node in which the mouse event occurred.
getSceneX() and getSceneY() return the horizontal and vertical position of the mouse event,
relative to the Scene .
getScreenX() and getScreenY() return the horizontal and vertical position of the mouse
event, relative to the screen.
Here are a few other commonly useful methods:
isDragDetect() returns true if a drag event is detected.
getButton() , isPrimaryButtonDown() , isSecondaryButtonDown() , isMiddleButtonDown() ,
and getClickCount() contain information about what button was clicked, and how many
times it was clicked.
A little later in this chapter you get some experience with creating key and mouse event handlers in the ZenPong
example program. To continue preparing you for the ZenPong example, we now give you a look at how you can
animate the nodes that you put in your scene.
 
Search WWH ::




Custom Search