Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX How to position the Mouse

I am trying to make a little game and in the most games the mouse gets locked in the center of the screen. So, is it possible to lock the mouse in the center of the screen or set the position of the mouse in JavaFX? I know that it is possible to do, and I also know some samples written in LWJGL or just with the AWT/SWING package.

Thanks for help.

like image 337
MrMechanik Avatar asked May 28 '16 15:05

MrMechanik


1 Answers

Update 11/27/2019

From now you can use also JavaFX Robot API: https://openjfx.io/javadoc/11/javafx.graphics/javafx/scene/robot/Robot.html


Here is the code you need:

import java.awt.AWTException;
import java.awt.Robot;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class MoveCursor extends Application {

Scene scene;
VBox container;
Button moveMouse;
Button showHideCursor;
public static int screenWidth = (int) Screen.getPrimary().getBounds().getWidth();
public static int screenHeight = (int) Screen.getPrimary().getBounds().getHeight();

@Override
public void start(Stage stage) throws Exception {

    // MoveMouse Button
    moveMouse = new Button("Move Cursor to the center of Screen");
    moveMouse.setOnAction(m -> {
        moveCursor(screenWidth/2, screenHeight/2);
    });

    // ShowHide Cursor
    showHideCursor = new Button("Show/Hide Cursor");
    showHideCursor.setCursor(Cursor.HAND);
    showHideCursor.setOnAction(m -> {
        if (scene.getCursor() != Cursor.NONE)
            scene.setCursor(Cursor.NONE);
        else
            scene.setCursor(Cursor.DEFAULT);
    });

    // Container
    container = new VBox();
    container.getChildren().addAll(moveMouse, showHideCursor);

    // Scene
    scene = new Scene(container, 500, 500);

    stage.setScene(scene);
    stage.show();
}

/**
 * Move the mouse to the specific screen position
 * 
 * @param x
 * @param y
 */
public void moveCursor(int screenX, int screenY) {
    Platform.runLater(() -> {
        try {
            Robot robot = new Robot();
            robot.mouseMove(screenX, screenY);
        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    });
}

public static void main(String[] args) {
    launch(args);
}

}
like image 72
GOXR3PLUS Avatar answered Oct 01 '22 22:10

GOXR3PLUS