# Java.awt.Robot coordinates

**URL:** <https://discourse.processing.org/t/java-awt-robot-coordinates/4967>\
**Category:** Coding Questions\
**Created:** [October 28, 2018, 11:37pm UTC](https://discourse.processing.org/t/java-awt-robot-coordinates/4967 "2018-10-28T23:37:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![germaine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/germaine/32/2103_2.png) [@germaine](https://discourse.processing.org/u/germaine)\
**Post date:** [October 28, 2018, 11:37pm UTC](https://discourse.processing.org/t/java-awt-robot-coordinates/4967/1 "2018-10-28T23:37:32Z")

</div>

I’ve been playing with the Robot.mouseMove() function to move the mouse cursor around. The x,y coordinates you put in the function move the cursor to any location on the screen.

However, I want the cursor to move from outside the Processing sketch window to a certain position on the sketch. But that doesn’t work, because mouseMove() doesn’t use the sketch window’s coordinate system.

Is there any way for me to refer to specifically the sketch window coordinates vs. the viewport coordinates?

Thanks so much!

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [October 29, 2018, 12:27am UTC](https://discourse.processing.org/t/java-awt-robot-coordinates/4967/2 "2018-10-29T00:27:03Z")

</div>

Although this is not natively possible as far as i know, you can simply calculate this position, as it is relative to the position of the sketch window. Just use instead of mouseMove(x, y); this : mouseMove(sketchX + x, sketchY + y); .This moves the mouse to the position of the sketches left upper corner and then to the position inside the sketch (actually it only moves it to the position inside the sketch, but the thinking behind sketchX moving it to start at the top left of the sketch and then x moves it further probably explains it a bit better).  
Here is a method to get the location of the window by @quark

```auto
PVector getWindowLocation(String renderer) {
  PVector l = new PVector();
  if (renderer == P2D || renderer == P3D) {
    com.jogamp.nativewindow.util.Point p = new com.jogamp.nativewindow.util.Point();
    ((com.jogamp.newt.opengl.GLWindow)surface.getNative()).getLocationOnScreen(p);
    l.x = p.getX();
    l.y = p.getY();
  } else if (renderer == JAVA2D) {
    java.awt.Frame f = (java.awt.Frame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
    l.x = f.getX();
    l.y = f.getY();
  }
  return l;
}

```

And here’s the question to it :

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/17675/how-to-get-surface-window-x-and-y-coordinates)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

So the code will look similar to this :

```auto
PVector windowPos = getWindowLocation("P2D");
Robot.mouseMove(windowPos.x + x, windowPos.y + y);

```

---

<div class="post-metadata">

**Author:** ![germaine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/germaine/32/2103_2.png) [@germaine](https://discourse.processing.org/u/germaine)\
**Post date:** [October 29, 2018, 2:15am UTC](https://discourse.processing.org/t/java-awt-robot-coordinates/4967/3 "2018-10-29T02:15:18Z")

</div>

Awesome! I will try this out.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 29, 2020, 8:31pm UTC](https://discourse.processing.org/t/java-awt-robot-coordinates/4967/4 "2020-01-29T20:31:02Z")

</div>

Did you succeed in this?

Thanks!

---

<div class="post-metadata">

**Author:** ![germaine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/germaine/32/2103_2.png) [@germaine](https://discourse.processing.org/u/germaine)\
**Post date:** [February 7, 2020, 7:46pm UTC](https://discourse.processing.org/t/java-awt-robot-coordinates/4967/5 "2020-02-07T19:46:43Z")

</div>

Yeah! I should try playing with that again
