Mouse Events

Paul Ngugi - Jun 24 - - Dev Community

A MouseEvent is fired whenever a mouse button is pressed, released, clicked, moved, or dragged on a node or a scene. The MouseEvent object captures the event, such as the number of clicks associated with it, the location (the x- and y-coordinates) of the mouse, or which mouse button was pressed, as shown in Figure below.

Image description

Four constants—PRIMARY, SECONDARY, MIDDLE, and NONE—are defined in MouseButton to indicate the left, right, middle, and none mouse buttons. You can use the getButton() method to detect which button is pressed. For example, getButton() == MouseButton.SECONDARY indicates that the right button was pressed.

The mouse events are listed in Table in this post. To demonstrate using mouse events, we give an example that displays a message in a pane and enables the message to be moved using a mouse. The message moves as the mouse is dragged, and it is always displayed at the mouse point. The program below gives the program. A sample run of the program is shown in Figure below.

Image description

Image description

Each node or scene can fire mouse events. The program creates a Text (line 13) and registers a handler to handle move dragged event (line 15). Whenever a mouse is dragged, the text’s x- and y-coordinates are set to the mouse position (lines 16 and 17).

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .