如何使用java模拟真正的鼠标点击? [英] How to simulate a real mouse click using java?

查看:572
本文介绍了如何使用java模拟真正的鼠标点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Java中执行鼠标单击,以单击外部程序中的某些内容。为此,我使用 java.awt.robot ,以及以下代码:

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:

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?

推荐答案

我有同样的确切的要求,机器人类对我来说完全没问题。它适用于Windows 7和XP(试过java 6& 7)。

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).

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?

这篇关于如何使用java模拟真正的鼠标点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆