简体   繁体   English

如何使用java模拟真实的鼠标点击?

[英]How to simulate a real mouse click using java?

I'm attempting to perform a mouse click in Java, to click something in an external program.我试图在 Java 中执行鼠标单击,以单击外部程序中的某些内容。 To do this, I'm using java.awt.robot , and the following code:为此,我使用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?我该怎么做才能将所有键盘/鼠标状态设置为与正常鼠标单击相同的方式?

Well I had the same exact requirement, and Robot class is perfectly fine for me.好吧,我有完全相同的要求,而 Robot 类对我来说非常好。 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_DOWN_MASK);
    bot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}

May be you could share the name of the program that is rejecting your click?也许您可以分享拒绝您点击的程序的名称?

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.仅供参考,在较新版本的 Windows 中,有一个新设置,如果一个程序在管理员模式下运行,那么另一个不在管理员模式下的程序无法向其发送任何点击或其他输入事件。 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.检查您尝试向其发送点击的源程序(右键单击 -> 属性),并查看是否选中了“以管理员身份运行”复选框。

it works in Linux.它适用于Linux。 perhaps there are system settings which can be changed in Windows to allow it.也许可以在 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;我假设InputEvent.MOUSE_BUTTON1_DOWN在Java的版本是一样的东西InputEvent.BUTTON1_DOWN_MASK矿山; I'm using 1.6.我正在使用 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在整个屏幕上平滑移动鼠标?

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 类(使用过很多次)应该发送操作系统无法与用户进行点击区分的消息。

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,它可以提供帮助。

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/

How to simulate a real mouse click with java with maximum clicks per second and minimum?如何用每秒最大点击次数和最小点击次数用java模拟真实的鼠标点击? enter image description here在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM