Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 709
ali Avatar asked Oct 04 '13 15:10

ali


1 Answers

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_DOWN_MASK);     bot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); } 

May be you could share the name of the program that is rejecting your click?

like image 173
AndyBaba Avatar answered Sep 20 '22 15:09

AndyBaba