Search code examples
javaawtrobot

Using a Robot to AutoClick?


I thought it would be easy to write a script to just simply Left Mouse click and then delay at random intervals each time 3-6, but when I run the code nothing seems to happen? at least no clicks? any insight that maybe i'm using Robot wrong? I have read the JavaDoc. Thanks!

EDIT 1: I fixed the inital question and have updated my now working code. My only other issue is how do i slow it down!!!! It is clicking at such a fast rate??

import java.awt.AWTException;
import java.awt.Robot;
import java.util.Random;
import java.awt.event.MouseEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main 
{
    public static Robot robot = null;

    public static void main(String[] args)
    {
        try {
            robot = new Robot();
        } catch (AWTException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        click(100000);

    }

    public static void click(int desiredAmount)
    {
        int counter = 0;
        int low = 3;
        int high = 6;


        Random rand = new Random();
        while (counter < desiredAmount)
        {
            robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
            robot.delay(rand.nextInt(high-low) + low);
            ++counter;
        }
    }
}

Solution

  • Is your loop condition correct? Shoudn't it be

    while (counter < desiredAmount)