Search code examples
javaawtrobot

How to check if left click is pressed after unpressing it from Robot class?


I'm making something like an auto clicker. I would like to make it so that when the left mouse button is pressed, the auto clicker should turn on. When the button is released, the auto clicker should turn off.

Is there a way to check if the left mouse click (in real life) is pressed after calling the robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK) method?


Solution

  • You should implement MouseListener interface in your class then override these two methods : mousePressed and mouseReleased .

    public void mousePressed(MouseEvent e) {
    //do the auto click code here
    }
    
    public void mouseReleased(MouseEvent e) {
      //turn off the auto click here
    }
    
         
    

    read this article