How to simulate a real mouse click using java? How to simulate a real mouse click using java? windows windows

How to simulate a real mouse click using java?


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?


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.


it works in Linux. perhaps there are system settings which can be changed in Windows to allow it.

jcomeau@aspire:/tmp$ cat test.java; javac test.java; java testimport 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.

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

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