Problems when using mouseClick and mouseRelease in Java Robot class

165 Views Asked by At

I am trying to figure out why I'm getting this exception.

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid combination of button flags

The code is simple. I am using a Robot class named Robot. I am trying to make it so the Robot will left click every 1 second as a test. However, I am having problems getting it to recognize the left click mouse button.

public void click() {
    try {
        robot = new Robot();
        robot.setAutoDelay(1000);
        robot.mousePress(MouseEvent.BUTTON1);
        robot.mouseRelease(MouseEvent.BUTTON1);

    } catch (AWTException e) {
        e.printStackTrace();
    }
}

I've read a few StackOverflow questions and they recommend using the getButtonMask() function, yet my library does not include that function.

1

There are 1 best solutions below

0
Krzysztof Cichocki On

You should use InputEvent instead of MouseEvent eg:

robot.mousePress(InputEvent.BUTTON1_MASK);