Fixing mouse position to window center

Is it possible to set the cursor position to the center (or anywhere else) of the screen? Similar to how games hide the cursor and don’t let it escape the window.

Context:
I’m making a 3D scene, and want the camera to follow the mouse as the user moves it.
I’m also not against tapping into OpenGL if needed.

1 Like

Yes, by using the Java Robot class:
https://forum.processing.org/one/topic/lock-the-cursor-in-the-window.html
https://docs.oracle.com/javase/6/docs/api/java/awt/Robot.html

Example code:

import java.awt.Robot;

Robot robot;

void setup () {
  size(600, 600);
  try {
    robot = new Robot();
  } 
  catch (Throwable e) {
  }
}

void draw() {
  robot.mouseMove(width/2, height/2);
}
1 Like

Robot and OpenGL (JOGL) can be unhappy bedfellows. Check out Problem with robot cursor for an alternative using JOGL’s support directly.

EDIT - or possibly this thread is more useful - Extending the mouse range past the screen size

2 Likes

Tanks for the help! I’ll give a more detailed answer and followup when I’ve done some research, around next Tuesday.