Java.awt.Robot coordinates

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!

1 Like

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

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 :

So the code will look similar to this :

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

Awesome! I will try this out.

Did you succeed in this?

Thanks!

1 Like

Yeah! I should try playing with that again

1 Like