Java bot for an online game

George Artemiou picture George Artemiou · Sep 1, 2012 · Viewed 11.8k times · Source

I am creating a bot in java using the java.awt.Robot. The bot works fine on a browser (I have also tested it using Microsoft Word!) but when I run it in the game, the only function that works is the mouseMove. I want to build a bot that simply presses keyboard buttons for me.

I instantiate the robot class

Robot r = new Robot();

Then I do some simple stuff: press z,press 1, move the mouse and right click.

r.keyPress(KeyEvent.VK_Z);
r.keyRelease(KeyEvent.VK_Z);

r.keyPress(KeyEvent.VK_1);
System.out.println("Press 1 button");
r.keyRelease(KeyEvent.VK_1);
System.out.println("Release 1 button");
r.delay(1000);

System.out.println("Move mouse");
r.mouseMove(110, 690);

System.out.println("Press");
r.mousePress(InputEvent.BUTTON3_MASK);
System.out.println("Release");
r.mouseRelease(InputEvent.BUTTON3_MASK);

Why is this happening? Can this Robot class perform these kind of actions within a game if it runs in the background?

Thank you

Update: If I run my bot on PES 2012 for example, it works fine but if I run it on an online game like Cabal, it does not work? the protection system of the game does not detect anything so that is not the case.

Answer

bruno picture bruno · Dec 22, 2012

First of all, most games have bot protection, so make sure to add a delay to the bot and, maybe, a 'cooldown'. Before that r.delay(1000) statement, the bot did two instant actions.

I'm almost sure it's not working because the keystrokes are way too fast: they press and release instantly. Try adding bot.delay(500) (or more, depends on the game) right after you instantiate Robot class; before all the key pressing functions. That would add a 500ms delay between ALL actions done by the robot.

public static void doStuff() {

    Robot r = new Robot();

        r.delay(500); //Or more - depends on the game

        r.keyPress(KeyEvent.VK_Z);
        r.keyRelease(KeyEvent.VK_Z);

        r.keyPress(KeyEvent.VK_1);
        System.out.println("Press 1 button");
        r.keyRelease(KeyEvent.VK_1);
        System.out.println("Release 1 button");
        r.delay(1000);

        System.out.println("Move mouse");
        r.mouseMove(110, 690);

        System.out.println("Press");
        r.mousePress(InputEvent.BUTTON3_MASK);
        System.out.println("Release");
        r.mouseRelease(InputEvent.BUTTON3_MASK);
}

I think the only reason why the Z and 1 keys didn't work was the speed everything was done. The game probably has an anti-bot system.