Robot class - If a Button Is Pressed?

2.9k views Asked by At

I have read and understood how the Robot class in java works. Only thing I would like to ask, is how do I press and release the mouse button inside an if statement. For example I would to make a click only if (and right after) the space button is pressed/released. I would use the code:

try {
  Robot robot = new Robot();
  if (/*insert my statement here*/) {
    try {
      robot.mousePress(InputEvent.BUTTON1_MASK);
      robot.mouseRelease(InputEvent.BUTTON1_MASK);
    } catch (InterruptedException ex) {}
  }
} catch (AWTException e) {}
2

There are 2 answers

1
BackSlash On BEST ANSWER

Unfortunately, there isn't a way to directly control hardware (well, in fact there is, but you would have to use JNI/JNA), this means that you can't simply check if a key is pressed.

You can use KeyBindings to bind the space key to an action, when the spacebar is pressed you set a flag to true, when it's released you set that flag to false. In order to use this solution, your application has to be a GUI application, this won't work with console applications.

Action pressedAction = new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        spaceBarPressed = true;
    }
};

Action releasedAction = new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        spaceBarPressed = false;
    }
};

oneOfYourComponents.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "pressed");
oneOfYourComponents.getInputMap().put(KeyStroke.getKeyStroke("released SPACE"), "released");
oneOfYourComponents.getActionMap().put("pressed", pressedAction);
oneOfYourComponents.getActionMap().put("released", releasedAction);

Then, use

try {
    Robot robot = new Robot();
    if (spaceBarPressed) {
        try {
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
        } catch (InterruptedException ex) {
            //handle the exception here
        }
    }
} catch (AWTException e) {
    //handle the exception here
}

As GGrec wrote, a better way to do it would be to execute your mouse press directly when the keyboard event is fired:

Action pressedAction = new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        try {
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
        } catch (InterruptedException ex) {
            //handle the exception here
        }
    }
};
0
Georgian On

My suggestion is that you listen for the keyboard event, and when you receive it, you execute your code without the if statement. Add the listener to your canvas, or whatever.

Careful not to recreate the Robot class each time.

new KeyAdapter() {

     @Override
     public void keyReleased(final KeyEvent e) {

           if (e.keyCode == KeyEvent.VK_SPACE)
                 try {
                     robot.mousePress(InputEvent.BUTTON1_MASK);
                     robot.mouseRelease(InputEvent.BUTTON1_MASK);
                 } catch (InterruptedException ex) {
                 }

     }

}