I am trying to get use a mouse event on button 4. The value returned when using the
int moused = MouseInfo.getNumberOfButtons();
method is 5. I can't work out what any of the buttons are besides button 4 which is right click; running the code I get this error:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid combinati
on of button flags
at java.awt.Robot.checkButtonsArgument(Robot.java:324)
at java.awt.Robot.mousePress(Robot.java:260)
at RobotExp4.main(RobotExp4.java:23)
import java.awt.*;
import java.awt.event.KeyEvent;
public class RobotExp4{
public static void main(String [] args){
try{
Robot robot = new Robot();
System.out.println("7 Seconds after this message appears the robot will start to open a browser and make a search.");
robot.delay(7000);
robot.keyPress(KeyEvent.VK_WINDOWS);
robot.keyRelease(KeyEvent.VK_WINDOWS);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.delay(150);
robot.keyPress(KeyEvent.VK_M);
robot.keyRelease(KeyEvent.VK_M);
robot.delay(150);
robot.keyPress(KeyEvent.VK_D);
robot.keyRelease(KeyEvent.VK_D);
robot.delay(5000);
robot.mouseMove(1340, 192);
robot.delay(200);
robot.mousePress(5);
robot.delay(75);
robot.mouseRelease(5);
}catch(AWTException e){
e.printStackTrace();
}
}
}
You need to pass mousePress
the result of calling InputEvent.getMaskForButton
, like this:
int mask4 = InputEvent.getMaskForButton(4);
robot.mousePress(mask4);
robot.delay(75);
robot.mouseRelease(mask4);
The reason why you need to pass a mask, as opposed to a button number, is to support combinations of buttons:
int mask1_and_2 = InputEvent.getMaskForButton(1) | InputEvent.getMaskForButton(2);
The mask above corresponds to pressing buttons 1 and 2 simultaneously.