Robot.mouseMove在Mac OS X中根本不起作用 [英] Robot.mouseMove does not work at all in Mac OS X
问题描述
我在IntelliJ IDE中使用该代码创建了Java文件
I created java file in IntelliJ IDE, with that code
import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Robot;
public class JavaRobotExample {
public static void main(String[] args) {
int x = 12,
y = 300;
try {
Robot robot = new Robot();
robot.mouseMove(x, y);
int xAct = (int) MouseInfo.getPointerInfo().getLocation().getX(),
yAct = (int) MouseInfo.getPointerInfo().getLocation().getY();
String sPred = String.format("Predicted mouse location : %, d, %, d", x, y),
sAct = String.format("Actual mouse location : %, d, %, d", xAct, yAct);
System.out.println(sPred);
System.out.println(sAct);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
该示例在Windows 7环境中运行良好,但是在Mac OS 10.14上,相同的代码不会使用mouseMove方法移动鼠标(但会通过MouseInfo类读取位置).
That example works fine in Windows 7 environment, but the same code on Mac OS 10.14 doesn't move mouse using method mouseMove (but reads position with MouseInfo class).
我也没有收到任何例外,例如
Also I don't receive any Exception e.g.
有人曾经有过类似的问题吗?关于如何使该代码在Mac OS中运行的任何想法吗?
Did someone had earlier similar problem? Any ideas how to get that code to work in Mac OS?
最好的问候,
推荐答案
我也遇到了同样的问题.我有一条语句"robot.mouseMove(100,100);"鼠标完全不动.我有一个研究思路.在运行代码时,我注意到来自MacOS的警告或信息类型的消息.问我有关授予我的程序控制屏幕或系统之类的权限的问题.我试图单击允许"复选框,但不允许我这样做.我将对此进行更多研究.我认为这是MacOS的事情.您(和我)必须告诉MacOS,我们的Java程序可以控制鼠标.
I'm having the same exact issue too. I have a statement "robot.mouseMove(100, 100);" and the mouse doesn't move at all. I have an idea to research. I noticed a warning or informational type of message from MacOS while I was running my code. It was asking me something about giving permission for my program to control the screen or system or something. I tried to click the "allow" checkbox, but it wouldn't let me. I'll research this some more. I think it's a MacOS thing. You (and I) have to tell MacOS that it's ok for our Java program to control the mouse.
我认为这是解决方案. .
I think this is the solution . . .
在系统偏好设置"(齿轮"图标)中的安全性"和隐私,单击顶部的隐私"选项卡,然后在左侧选择辅助功能".这列出了所有可以控制您的计算机"的程序.我正在使用STS,而不是IntelliJ.我看到列出了STS以及BetterSnapTool和KeyCastr.同时检查BetterSnapTool和KeyCastr.未检查STS.而且,所有这些均灰显,因此我无法更改任何支票.窗口左下方有一个锁定"图标.我单击该图标,然后出现提示询问我的密码(我具有一定级别的sysadmin特权).我输入了密码,现在可以检查" STS.我检查"了STS(您将检查" IntelliJ).在检查" STS之后,我再次单击左下方的锁定"图标.这关闭"了锁.现在,当我运行程序时,robot命令会移动鼠标.
In System Preferences (the "gear" icon), under Security & Privacy, click the Privacy tab toward the top, then choose Accessibility on the left. This lists all the programs that can "control your computer". I'm using STS, not IntelliJ. I see STS listed along with BetterSnapTool and KeyCastr. Both BetterSnapTool and KeyCastr are checked. STS is not checked. And, all of these are grayed out so that I can't change any of the checks. There's a Lock icon at the bottom left of the window. I clicked on the icon, and a prompt came up asking for my password (I have some level of sysadmin privileges). I put in my password, and I am now allowed to "check" STS. I "checked" STS (you will "check" IntelliJ). After "checking" STS, I clicked the Lock icon at the bottom left again. This "closed" the lock. Now when I run my program, the robot command moves my mouse.
这篇关于Robot.mouseMove在Mac OS X中根本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!