Skip to content

Commit 461cb84

Browse files
author
Alisen Chung
committed
8345538: Robot.mouseMove doesn't clamp bounds on macOS when trying to move mouse off screen
Reviewed-by: honkar, prr
1 parent c382da5 commit 461cb84

File tree

2 files changed

+121
-2
lines changed

2 files changed

+121
-2
lines changed

src/java.desktop/macosx/classes/sun/lwawt/macosx/CRobot.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,8 @@
2525

2626
package sun.lwawt.macosx;
2727

28+
import java.awt.GraphicsDevice;
29+
import java.awt.GraphicsEnvironment;
2830
import java.awt.Point;
2931
import java.awt.Rectangle;
3032
import java.awt.peer.RobotPeer;
@@ -63,7 +65,41 @@ public void mouseMove(int x, int y) {
6365
mouseLastX = x;
6466
mouseLastY = y;
6567

66-
mouseEvent(mouseLastX, mouseLastY, mouseButtonsState, true, true);
68+
int leastDiff = Integer.MAX_VALUE;
69+
int finX = x;
70+
int finY = y;
71+
72+
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
73+
GraphicsDevice[] gs = ge.getScreenDevices();
74+
Rectangle[] allScreenBounds = new Rectangle[gs.length];
75+
76+
for (int i = 0; i < gs.length; i++) {
77+
allScreenBounds[i] = gs[i].getDefaultConfiguration().getBounds();
78+
}
79+
80+
for (Rectangle screenBounds : allScreenBounds) {
81+
Point cP = calcClosestPoint(x, y, screenBounds);
82+
83+
int currXDiff = Math.abs(x - cP.x);
84+
int currYDiff = Math.abs(y - cP.y);
85+
int currDiff = (int) Math.round(Math.hypot(currXDiff, currYDiff));
86+
87+
if (currDiff == 0) {
88+
mouseEvent(mouseLastX, mouseLastY, mouseButtonsState, true, true);
89+
return;
90+
} if (currDiff < leastDiff) {
91+
finX = cP.x;
92+
finY = cP.y;
93+
leastDiff = currDiff;
94+
}
95+
}
96+
97+
mouseEvent(finX, finY, mouseButtonsState, true, true);
98+
}
99+
100+
private Point calcClosestPoint(int x, int y, Rectangle screenBounds) {
101+
return new Point(Math.min(Math.max(x, screenBounds.x), screenBounds.x + screenBounds.width - 1),
102+
Math.min(Math.max(y, screenBounds.y), screenBounds.y + screenBounds.height - 1));
67103
}
68104

69105
/**
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.awt.GraphicsConfiguration;
25+
import java.awt.GraphicsDevice;
26+
import java.awt.GraphicsEnvironment;
27+
import java.awt.MouseInfo;
28+
import java.awt.Point;
29+
import java.awt.Rectangle;
30+
import java.awt.Robot;
31+
32+
/*
33+
* @test
34+
* @bug 8345538
35+
* @summary Tests mouseMove clamping to screen bounds when set to move offscreen
36+
* @requires (os.family == "mac")
37+
* @key headful
38+
* @run main MouseMoveOffScreen
39+
*/
40+
41+
public class MouseMoveOffScreen {
42+
private static final Point STARTING_LOC = new Point(200, 200);
43+
private static final Point OFF_SCREEN_LOC = new Point(20000, 200);
44+
private static Rectangle[] r;
45+
46+
public static void main(String[] args) throws Exception {
47+
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
48+
GraphicsDevice[] gs = ge.getScreenDevices();
49+
r = new Rectangle[gs.length];
50+
51+
for (int i = 0; i < gs.length; i++) {
52+
r[i] = gs[i].getDefaultConfiguration().getBounds();
53+
System.out.println("Screen: "+ gs[i].getIDstring() + " Bounds: " + r[i]);
54+
}
55+
56+
Point offsc = validateOffScreen(OFF_SCREEN_LOC);
57+
Robot robot = new Robot();
58+
robot.mouseMove(STARTING_LOC.x, STARTING_LOC.y);
59+
robot.delay(500);
60+
robot.mouseMove(offsc.x, offsc.y);
61+
robot.delay(500);
62+
63+
Point currLoc = MouseInfo.getPointerInfo().getLocation();
64+
65+
if (currLoc == null) {
66+
throw new RuntimeException("Test Failed, getLocation returned null.");
67+
}
68+
69+
System.out.println("Current mouse location: " + currLoc);
70+
if (currLoc.equals(OFF_SCREEN_LOC)) {
71+
throw new RuntimeException("Test Failed, robot moved mouse off screen.");
72+
}
73+
}
74+
75+
private static Point validateOffScreen(Point p) {
76+
for (Rectangle rect : r) {
77+
if (rect.contains(p)) {
78+
return validateOffScreen(new Point(p.x * 2, p.y));
79+
}
80+
}
81+
return p;
82+
}
83+
}

0 commit comments

Comments
 (0)