Gerret
Gerret

Reputation: 3046

What is the right way to move the mouse in a game?

I want to program a bot for minecraft that automatically collects items. But I have a problem to move the mouse in the game. The movement acts strange... it is jumping around at the x and y coordination even if I only add 1 to the y coordinate. The movment acts like this in every 3D game not only in minecraft.

For the movement I use the integreated robot class.

Here is the snipped I use for the mouse movement:

public static void main(String[] args) {

    try {
        Robot bot = new Robot();

        Point mouseposition = MouseInfo.getPointerInfo().getLocation();
        int x = mouseposition.x;
        int y = mouseposition.y;

        //used to switch to the game window
        bot.delay(5000);

        y += 1;

        bot.mouseMove(x, y);

    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

What I was expecting with this code was, that the course just move one pixel down. The cursor is moving fine if I am on the desktop!

System:

  • os: Windows 8.1
  • arch: amd64
  • javaversion: 1.7.0_67
  • ide: eclipse luna

Upvotes: 2

Views: 1245

Answers (1)

Sizik
Sizik

Reputation: 1066

You're capturing the mouse location before you switch to the game window, so when you add 1 to the y coordinate, you're actually adding 1 to where the mouse was before you moved it to switch to the game window. Put the delay before the Point mouseposition = MouseInfo.getPointerInfo().getLocation(); line instead.

Upvotes: 2

Related Questions