7

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.

asked May 28, 2016 at 15:04
5
  • 1)In Swing you can do this using Robot,an example here:programmersheaven.com/discussion/306819/… 2)Maybe making the mouse transparent is a solution for you? Commented May 29, 2016 at 6:39
  • I'm looking for exactly the same, please share a solution if you've found one Commented May 29, 2016 at 11:31
  • @GoXr3Plus Thank you for responding, but sadly the MouseTransparency is not what I'm looking for. Commented May 29, 2016 at 14:31
  • You want to move the mouse programmatically in javaFX?That's the whole point? Commented May 29, 2016 at 20:27
  • @GoXr3Plus well, yes I'm trying to, as said, "set" the position of the Mouse while the transpareny only causes to disable the MouseEvents on the Node. Commented May 30, 2016 at 20:14

1 Answer 1

7

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);
}
}
answered May 31, 2016 at 0:38
Sign up to request clarification or add additional context in comments.

3 Comments

awesome! I didn't knew that you can use AWT in FX. Thank you so much!
JavaFX is missing(for now) some basic things for example(Robot and ImageIO) that AWT has.Generally try to avoid mixing these two cause they are running in different threads and may cause problems...Thats why i am using Platform.runLater in the example()..

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.