Java bot for an online game
Asked Answered
M

3

7

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.

Mneme answered 31/8, 2012 at 23:17 Comment(8)
Games often times have 'defenses' against bots - just a possibility.Calender
Try adding a delay of a few milliseconds between the keyPress and keyRelease events. If this is a normal arcade-style game, then I bet the game is just polling each tick to see whether the key is currently down, not capturing instantaneous events like "key pressed" and "key released".Pairs
tried that and its not working...Mneme
possible duplicate of Java: use Robot while running fullscreen appKrystakrystal
maybe not, I think this guy has a problem with the bot running in the game at all...I think it's the game's defense system against bots too.Leprosy
The issue here is that the game defence system does NOT detect this bot. If it did, it would have kicked me out. I am wondering, does it matter in which language the game was written? Or is there a specific language that will help me solve this issue? Thank youMneme
@Iced_Earth A lot of online games nowadays don't ban you right off the bat - bot detection merely triggers elevated logging just to see what you're trying to accomplish so that the devs can focus on making those parts extra safe and fair for the rest of the player base. In short, I think you're being immoral which is possibly the worst thing a developer can be.Nobie
thank you Esko for your reply. I am not being immoral I just want to understand how thinks work.Mneme
S
5

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.

Syncarpous answered 22/12, 2012 at 3:38 Comment(0)
G
2

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.

Gracious answered 6/9, 2012 at 19:46 Comment(1)
nop. did not work I have added some delay between the key up event triggersMneme
T
0

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!

Tittup answered 23/6, 2020 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.