RFR: 8345538: Robot.mouseMove doesn't clamp bounds on macOS when trying to move mouse off screen [v6]
Alexey Ivanov
aivanov at openjdk.org
Fri Jan 31 19:54:11 UTC 2025
On Thu, 30 Jan 2025 20:21:16 GMT, Alisen Chung <achung at openjdk.org> wrote:
> > I would like to clarify one point: if the robot moves the mouse off the screen while the actual mouse pointer is on the screen and immediately presses the mouse button, where will the click occur? on or off the screen?
>
> In macOS code there is similar method that checks for mouse offscreen locations called checkMousePos() which updates and clamps the mousePosition stored in CRobot and is called after mousePress and mouseRelease
In macOS? Do you mean in macOS-specific code in JDK?
~~If the code to clamp mouse coordinates has already been written, why are you writing new code instead of re-using the existing code?~~
However, the code in [`CRobot.checkMousePos`](https://github.com/openjdk/jdk/blob/651ac3cc0f2a8b3edf5cddb42df1d38d4aa0e1a6/src/java.desktop/macosx/classes/sun/lwawt/macosx/CRobot.java#L98) doesn't really clamp mouse coordinates.
The value of `MOUSE_LOCATION_UNKNOWN` is -1:
https://github.com/openjdk/jdk/blob/651ac3cc0f2a8b3edf5cddb42df1d38d4aa0e1a6/src/java.desktop/macosx/classes/sun/lwawt/macosx/CRobot.java#L36
This means it may be impossible to click at (-1, -1) which could be perfectly valid coordinates. It looks like _a bug_, actually.
> > nonexistent coordinates.
>
> But why this coordinates nonexistent, you at least can move undecorated windows there, that coordinates just is not visible on teh screen.
This is the question we have to answer first: Are the coordinates off the virtual screen invalid, non-existent?
What we've seen so far is `Robot.mouseMove` succeeds in a way that mouse appears to be moved to the specified location. That location gets returned by the OS, at least on macOS, when the OS-specific native code in `MouseInfo.getPointerInfo()` reads the coordinates — it is the code in `MouseInfo.getPointerInfo()` that clamps the coordinates to known screen devices and returns `null`, if the coordinates are outside of these bounds:
https://github.com/openjdk/jdk/blob/651ac3cc0f2a8b3edf5cddb42df1d38d4aa0e1a6/src/java.desktop/share/classes/java/awt/MouseInfo.java#L84-L86
This is because `PointerInfo` requires both the device and the coordinates.
Then, when you use a physical mouse, the OS doesn't allow moving the mouse cursor outside of the virtual screen bounds. Does this imply Java should prevent moving the mouse cursor off the virtual screen, too?
-------------
PR Comment: https://git.openjdk.org/jdk/pull/22781#issuecomment-2628256432
More information about the client-libs-dev
mailing list