I'm attempting to perform a mouse click in Java, to click something in an external program. To do this, I'm using java.awt.robot
, and the following code:
我正在尝试在Java中执行鼠标单击,以单击外部程序中的某些内容。为此,我使用的是java.awt.robot,以及以下代码:
Robot bot = new Robot();
int mask = InputEvent.MOUSE_BUTTON1_DOWN;
bot.mouseMove(x, y);
bot.mousePress(mask);
bot.mouseRelease(mask);
Here's the problem. The external program is able to detect that this click is computer-generated and not human-generated, and hence, its rejecting this click.
这是问题所在。外部程序能够检测到此单击是由计算机生成的而不是人为生成的,因此,它拒绝此单击。
I have already tried moving the mouse there naturally and that didn't have any effect. So my guess is, that it must be listening to the keyboard state or such, and telling from that, that the click is computer generated.
我已经尝试过自然地移动鼠标,但没有任何效果。所以我的猜测是,它必须正在听键盘状态等,并告诉它,点击是由计算机生成的。
What do I have to do to set all keyboard / mouse states to act in the same way as a normal mouse click would?
如何将所有键盘/鼠标状态设置为与正常鼠标单击相同的方式,我该怎么办?
6 个解决方案
#1
42
Well I had the same exact requirement, and Robot class is perfectly fine for me. It works on windows 7 and XP (tried java 6 & 7).
好吧,我有同样的要求,机器人类对我来说非常好。它适用于Windows 7和XP(试过java 6和7)。
public static void click(int x, int y) throws AWTException{
Robot bot = new Robot();
bot.mouseMove(x, y);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
}
May be you could share the name of the program that is rejecting your click?
您可以分享拒绝点击的程序名称吗?
#2
11
FYI, in newer versions of Windows, there's a new setting where if a program is running in Adminstrator mode, then another program not in administrator mode, cannot send any clicks or other input events to it. Check your source program to which you are trying to send the click (right click -> properties), and see if the 'run as administrator' checkbox is selected.
仅供参考,在较新版本的Windows中,有一个新设置,如果程序在管理员模式下运行,则另一个不在管理员模式下的程序无法向其发送任何点击或其他输入事件。检查您尝试发送点击的源程序(右键单击 - >属性),然后查看是否选中了“以管理员身份运行”复选框。
#3
5
it works in Linux. perhaps there are system settings which can be changed in Windows to allow it.
它适用于Linux。也许有一些系统设置可以在Windows中更改以允许它。
jcomeau@aspire:/tmp$ cat test.java; javac test.java; java test
import java.awt.event.*;
import java.awt.Robot;
public class test {
public static void main(String args[]) {
Robot bot = null;
try {
bot = new Robot();
} catch (Exception failed) {
System.err.println("Failed instantiating Robot: " + failed);
}
int mask = InputEvent.BUTTON1_DOWN_MASK;
bot.mouseMove(100, 100);
bot.mousePress(mask);
bot.mouseRelease(mask);
}
}
I'm assuming InputEvent.MOUSE_BUTTON1_DOWN
in your version of Java is the same thing as InputEvent.BUTTON1_DOWN_MASK
in mine; I'm using 1.6.
我假设您的Java版本中的InputEvent.MOUSE_BUTTON1_DOWN与我的InputEvent.BUTTON1_DOWN_MASK相同;我正在使用1.6。
otherwise, that could be your problem. I can tell it worked because my Chrome browser was open to http://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html when I ran the program, and it changed to Debian.org because that was the link in the bookmarks bar at (100, 100).
否则,这可能是你的问题。我可以告诉它有效,因为当我运行该程序时,我的Chrome浏览器对http://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html开放,并且它已更改为Debian.org因为这是(100,100)书签栏中的链接。
[added later after cogitating on it today] it might be necessary to trick the listening program by simulating a smoother mouse movement. see the answer here: How to move a mouse smoothly throughout the screen by using java?
[今天在考虑之后补充]可能有必要通过模拟更平滑的鼠标移动来欺骗听力程序。在这里看到答案:如何使用java在整个屏幕上平滑移动鼠标?
#4
3
With all respect the most likely thing is that you are mistaken about why the click is being 'rejected'. Why do you think some program is trying to determine if it's human or not? The Robot class (have used it a lot) should send messages that the operating system has no way to distinguish from a user doing the click.
在所有方面,最可能的是你错误地认为点击被“拒绝”的原因。为什么你认为某个程序试图确定它是否是人类? Robot类(已经使用了很多)应该发送操作系统无法区分用户进行点击的消息。
#5
0
Some applications may detect click source at low OS level. If you really need that kind of hack, you may just run target app in virtual machine's window, and run cliker in host OS, it can help.
某些应用程序可能会检测低OS级别的点击源。如果你真的需要那种黑客攻击,你可以在虚拟机的窗口中运行目标应用程序,并在主机操作系统中运行cliker,它可以提供帮助。
#6
0
You could create a simple AutoIt Script that does the job for you, compile it as an executable and perform a system call there.
您可以创建一个简单的AutoIt脚本,为您完成工作,将其编译为可执行文件并在那里执行系统调用。
in au3 Script:
在au3脚本:
; how to use: MouseClick ( "button" [, x, y [, clicks = 1 [, speed = 10]]] )
MouseClick ( "left" , $CmdLine[1], $CmdLine[1] )
Now find aut2exe in your au3 Folder or find 'Compile Script to .exe' in your Start Menu and create an executable.
现在在au3文件夹中找到aut2exe,或在“开始”菜单中找到“将脚本编译为.exe”并创建可执行文件。
in your Java class call:
在您的Java类调用中:
Runtime.getRuntime().exec(
new String[]{
"yourscript.exe",
String.valueOf(mypoint.x),
String.valueOf(mypoint.y)}
);
AutoIt will behave as if it was a human and won't be detected as a machine.
AutoIt的行为就像是人类一样,不会被检测为机器。
Find AutoIt here: https://www.autoitscript.com/
在这里找到AutoIt:https://www.autoitscript.com/
#1
42
Well I had the same exact requirement, and Robot class is perfectly fine for me. It works on windows 7 and XP (tried java 6 & 7).
好吧,我有同样的要求,机器人类对我来说非常好。它适用于Windows 7和XP(试过java 6和7)。
public static void click(int x, int y) throws AWTException{
Robot bot = new Robot();
bot.mouseMove(x, y);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
}
May be you could share the name of the program that is rejecting your click?
您可以分享拒绝点击的程序名称吗?
#2
11
FYI, in newer versions of Windows, there's a new setting where if a program is running in Adminstrator mode, then another program not in administrator mode, cannot send any clicks or other input events to it. Check your source program to which you are trying to send the click (right click -> properties), and see if the 'run as administrator' checkbox is selected.
仅供参考,在较新版本的Windows中,有一个新设置,如果程序在管理员模式下运行,则另一个不在管理员模式下的程序无法向其发送任何点击或其他输入事件。检查您尝试发送点击的源程序(右键单击 - >属性),然后查看是否选中了“以管理员身份运行”复选框。
#3
5
it works in Linux. perhaps there are system settings which can be changed in Windows to allow it.
它适用于Linux。也许有一些系统设置可以在Windows中更改以允许它。
jcomeau@aspire:/tmp$ cat test.java; javac test.java; java test
import java.awt.event.*;
import java.awt.Robot;
public class test {
public static void main(String args[]) {
Robot bot = null;
try {
bot = new Robot();
} catch (Exception failed) {
System.err.println("Failed instantiating Robot: " + failed);
}
int mask = InputEvent.BUTTON1_DOWN_MASK;
bot.mouseMove(100, 100);
bot.mousePress(mask);
bot.mouseRelease(mask);
}
}
I'm assuming InputEvent.MOUSE_BUTTON1_DOWN
in your version of Java is the same thing as InputEvent.BUTTON1_DOWN_MASK
in mine; I'm using 1.6.
我假设您的Java版本中的InputEvent.MOUSE_BUTTON1_DOWN与我的InputEvent.BUTTON1_DOWN_MASK相同;我正在使用1.6。
otherwise, that could be your problem. I can tell it worked because my Chrome browser was open to http://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html when I ran the program, and it changed to Debian.org because that was the link in the bookmarks bar at (100, 100).
否则,这可能是你的问题。我可以告诉它有效,因为当我运行该程序时,我的Chrome浏览器对http://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html开放,并且它已更改为Debian.org因为这是(100,100)书签栏中的链接。
[added later after cogitating on it today] it might be necessary to trick the listening program by simulating a smoother mouse movement. see the answer here: How to move a mouse smoothly throughout the screen by using java?
[今天在考虑之后补充]可能有必要通过模拟更平滑的鼠标移动来欺骗听力程序。在这里看到答案:如何使用java在整个屏幕上平滑移动鼠标?
#4
3
With all respect the most likely thing is that you are mistaken about why the click is being 'rejected'. Why do you think some program is trying to determine if it's human or not? The Robot class (have used it a lot) should send messages that the operating system has no way to distinguish from a user doing the click.
在所有方面,最可能的是你错误地认为点击被“拒绝”的原因。为什么你认为某个程序试图确定它是否是人类? Robot类(已经使用了很多)应该发送操作系统无法区分用户进行点击的消息。
#5
0
Some applications may detect click source at low OS level. If you really need that kind of hack, you may just run target app in virtual machine's window, and run cliker in host OS, it can help.
某些应用程序可能会检测低OS级别的点击源。如果你真的需要那种黑客攻击,你可以在虚拟机的窗口中运行目标应用程序,并在主机操作系统中运行cliker,它可以提供帮助。
#6
0
You could create a simple AutoIt Script that does the job for you, compile it as an executable and perform a system call there.
您可以创建一个简单的AutoIt脚本,为您完成工作,将其编译为可执行文件并在那里执行系统调用。
in au3 Script:
在au3脚本:
; how to use: MouseClick ( "button" [, x, y [, clicks = 1 [, speed = 10]]] )
MouseClick ( "left" , $CmdLine[1], $CmdLine[1] )
Now find aut2exe in your au3 Folder or find 'Compile Script to .exe' in your Start Menu and create an executable.
现在在au3文件夹中找到aut2exe,或在“开始”菜单中找到“将脚本编译为.exe”并创建可执行文件。
in your Java class call:
在您的Java类调用中:
Runtime.getRuntime().exec(
new String[]{
"yourscript.exe",
String.valueOf(mypoint.x),
String.valueOf(mypoint.y)}
);
AutoIt will behave as if it was a human and won't be detected as a machine.
AutoIt的行为就像是人类一样,不会被检测为机器。
Find AutoIt here: https://www.autoitscript.com/
在这里找到AutoIt:https://www.autoitscript.com/