Skip to content

Mouse listener

Pegacraffft edited this page Oct 28, 2020 · 1 revision

Mouse listener

The mouse listener is used to interact with your scene via the mouse. This class doesn't need to be initialized. You can access it via the mouseListener variable in the scene .

1. addEvent(MouseButtons button, Predicate function)

addEvent(MouseButtons button, Consumer function, boolean blocking)

This method is used to add an event to the scene. It executes a set of commands if a certain mouse button is pressed.

MouseButtons button: This is the mouse button you want to be pressed. It uses an enum named MouseButtons to simplify the process of adding an mouse button to the event. (e. g. MouseButtons.LEFT_CLICK)

Predicate<MouseEvent> function: The set of commands you want to execute (e. g. e -> {System.out.println("Hello")}).

boolean blocking: If true: disables all processing of the mouse button. If false: enables all processing of the mouse button.

Examples:

mouseListener.addEvent(MouseButtons.LEFT_KLICK, e -> {
    System.out.println("Left mouse button pressed!");
});
mouseListener.addEvent(MouseButtons.LEFT_KLICK, e -> {
    System.out.println("Left mouse button pressed!");
}, true);

2. deleteEvent(MouseButtons button, Predicate function)

This method is used to remove an event in the scene.

Parameters:

MouseButtons button: This is the mouse button of the event you want to be deleted. It uses an enum named MouseButtons to simplify the process of removing an mouse button from the program. (e. g. MouseButtons.LEFT_CLICK)

Examples:

//Add an mouse event.
mouseListener.addEvent(MouseButtons.LEFT_KLICK, LClick());

//Remove the mouse event.
mouseListener.deleteEvent(MouseButtons.LEFT_KLICK, LClick());

//The method that gets executed by the mouse event
private void LClick(){
    System.out.println("Left mouse button pressed!");
}

3. isMouseInside()

This method returns true if the mouse is inside the window and false if not.

Examples:

if(mouseListener.isMouseInside()){
    System.out.println("Mouse is inside!");
}

4. isHeld(int mouseButton)

This method returns true if the specified mouse button is held down and false if not.

Parameters:

int mouseButton: The mouse button you want to be held. (e. g. 1 for the left mouse button)

Examples:

if(mouseListener.isHeld(1)){
    System.out.println("Left mouse button is held!");
}

Getter and setter:

getMousePos()

Returns the position of the mouse as a Point.