Oshrib
Oshrib

Reputation: 1900

JavaFX set mouse hidden when idle

How can i hide the mouse on JavaFX application, when the user is idle, lets say for 1 sec ? and show it again when the mouse move?

I have this part of code

scene.setCursor(Cursor.NONE);

But i do not know how to link it to idle time.

Upvotes: 1

Views: 628

Answers (1)

James_D
James_D

Reputation: 209553

You can do something like this:

PauseTransition idle = new PauseTransition(Duration.seconds(1));
idle.setOnFinished(e -> scene.setCursor(Cursor.NONE));
scene.addEventHandler(Event.ANY, e -> {
    idle.playFromStart();
    scene.setCursor(Cursor.DEFAULT);
});

This creates a one-second pause. When the user performs any action, the pause is restarted and the cursor set to the default. If the pause finishes, which can only happen if it wasn't restarted for the whole duration (i.e. if the user did nothing for a second), then the cursor is set to NONE.

Upvotes: 4

Related Questions