Q:

Java simple mouse macro for fireground application

So I'm trying to create a program that I can use as a mouse macro for a video game (just click the mouse 20 times every 500ms). When I run the program it works, but not on the game. How do I make it run for any foreground application?
import java.awt.Robot;
import java.awt.event.InputEvent;

public class MainClass {

    public static void main(String[] args) throws Exception{

        Robot r = new Robot();
        for (int n = 0; n < 20; n++) {
            r.mousePress( InputEvent.BUTTON1_MASK );
            r.mouseRelease( InputEvent.BUTTON1_MASK );
            Thread.sleep(500);
            r.mousePress( InputEvent.BUTTON1_MASK );
            r.mouseRelease( InputEvent.BUTTON1_MASK );
        }
    }
}

A:

The robot will click on the active window, same as if you were wlicking with you own mouse. 
The active program is your IDE when you launch the program, so you need to launch it, then go on your game (it sets it as active window) and the robot will click on it, so better sleep like 2sec to give you the time to switch on your game : 
public static void main(String[] args) throws Exception{

    Robot r = new Robot();
    Thread.sleep(2000);             // <---- switch window
    for (int n = 0; n < 20; n++) {
        // ...
    }
}

