How to simulate a real mouse click using java?

前端 未结 6 1600
清酒与你
清酒与你 2020-11-27 14:44

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:

6条回答
  •  迷失自我
    2020-11-27 15:32

    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?

提交回复
热议问题