如何使用java模拟真实的鼠标点击?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19185162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to simulate a real mouse click using java?
提问by ali
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?
我该怎么做才能将所有键盘/鼠标状态设置为与正常鼠标单击相同的方式?
回答by jcomeau_ictx
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.
我假设InputEvent.MOUSE_BUTTON1_DOWN
您的 Java 版本与我的版本相同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.htmlwhen 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在整个屏幕上平滑移动鼠标?
回答by jcomeau_ictx
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 类(使用过很多次)应该发送操作系统无法与用户进行点击区分的消息。
回答by AndyBaba
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).
好吧,我有完全相同的要求,而 Robot 类对我来说非常好。它适用于 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_DOWN_MASK);
bot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
May be you could share the name of the program that is rejecting your click?
也许您可以分享拒绝您点击的程序的名称?
回答by Click Upvote
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 中,有一个新设置,如果一个程序在管理员模式下运行,那么另一个不在管理员模式下的程序将无法向其发送任何点击或其他输入事件。检查您尝试向其发送点击的源程序(右键单击 -> 属性),并查看是否选中了“以管理员身份运行”复选框。
回答by Taras
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.
某些应用程序可能会在低操作系统级别检测点击源。如果你真的需要那种 hack,你可以在虚拟机的窗口中运行目标应用程序,并在主机操作系统中运行 cliker,它可以提供帮助。
回答by Thomas Raffelsieper
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/