javaawtrobot

Simulate Mouse Click in Java in DirectX Game


I am currently using Java's Robot Class within a DirectX game programmed in C++. I can successfully use the Robot class's mouseMove method, but when I try to use the mouse left click input event, nothing happens. I have tried different time spacings between the release and press to no avail. Note: I am currently running eclipse in administrator mode. Here is the code:

public class test {
    public static void main(String [] args) throws AWTException, I nterruptedException{
    Robot r = new Robot();

    Thread.sleep(3000);
    for(int i = 0; i<20; i++){
        r.mouseMove(100+i*50, 550);
        Thread.sleep(1);
    }

    Thread.sleep(1000);
    r.mousePress(InputEvent.BUTTON1_MASK);
    Thread.sleep(50);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
    Thread.sleep(50);
    r.mousePress(InputEvent.BUTTON1_MASK);
    Thread.sleep(50);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
    Thread.sleep(50);
  }
}

Any idea on how to get the mouse click to register?


Solution

  • In some games you just cant do this.Depends on the engine and implementation , for instance in source engine games your events will be registered(probably,based on my experience example :CS:GO) , bud unreal engine games might not register anything.

    Its really common for game developers to actually block some calls of WIN32 mouse_event , which is what Robot API uses.So there is not much you can do to go around it(with Robot api).