Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

It depends greatly on what type of game it is. If the code is simply emulating system input like keyboard actions. It should just look like a regular person.

However from what it looks like. From your example. Its running at lightning speed thus its prob not detecting the input at all, and/or the anti bot measures on so called game you are trying to bot. Is blocking input. Put delays into the mix. See if that helps. Ill be back for more help. I'm not professional on this. But its my best guess.

EDIT:

When I mean delay put a delay before the key up events to fire.. That way it has time to process the keys.

Your code might be pressing and releasing the keys too quickly for the game. There are often Games which require you to press and release the key with at least a 40ms-80ms delay. Trying this should help -

public static void doSomething() throws Exception{
    Robot rbt = new Robot();
    int typingTimes = 20;
    while(timer-- > 0){
        rbt.keyPress(KeyEvent.VK_1);
        //You can use Thread.sleep() after each keyPress to increase the delay
        Thread.sleep(500);
        rbt.keyRelease(KeyEvent.VK_1);
        
        rbt.keyPress(KeyEvent.VK_Z);
        //You can use Robot's delay() method after each keyPress too
        rbt.delay(500);
        rbt.keyRelease(KeyEvent.VK_Z);
    }
}

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top