# Fixing mouse position to window center

**URL:** <https://discourse.processing.org/t/fixing-mouse-position-to-window-center/6023>\
**Category:** Coding Questions\
**Created:** [November 28, 2018, 9:33am UTC](https://discourse.processing.org/t/fixing-mouse-position-to-window-center/6023 "2018-11-28T09:33:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![djokjula](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/djokjula/32/2544_2.png) [@djokjula](https://discourse.processing.org/u/djokjula)\
**Post date:** [November 28, 2018, 9:33am UTC](https://discourse.processing.org/t/fixing-mouse-position-to-window-center/6023/1 "2018-11-28T09:33:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [November 28, 2018, 10:11am UTC](https://discourse.processing.org/t/fixing-mouse-position-to-window-center/6023/2 "2018-11-28T10:11:53Z")

</div>

Yes, by using the Java Robot class:  
[https://forum.processing.org/one/topic/lock-the-cursor-in-the-window.html](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](https://docs.oracle.com/javase/6/docs/api/java/awt/Robot.html)

Example code:

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [November 28, 2018, 11:18am UTC](https://discourse.processing.org/t/fixing-mouse-position-to-window-center/6023/3 "2018-11-28T11:18:42Z")

</div>

Robot and OpenGL (JOGL) can be unhappy bedfellows. Check out [Problem with robot cursor](https://discourse.processing.org/t/problem-with-robot-cursor/2046) for an alternative using JOGL’s support directly.

EDIT - or possibly this thread is more useful - [Extending the mouse range past the screen size](https://discourse.processing.org/t/extending-the-mouse-range-past-the-screen-size/4431/3)

---

<div class="post-metadata">

**Author:** ![djokjula](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/djokjula/32/2544_2.png) [@djokjula](https://discourse.processing.org/u/djokjula)\
**Post date:** [November 28, 2018, 11:46pm UTC](https://discourse.processing.org/t/fixing-mouse-position-to-window-center/6023/4 "2018-11-28T23:46:18Z")

</div>

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