Imri Persiado
Imri Persiado

Reputation: 1889

Java bot using the Robot class

I'm trying to create a game bot using the Robot class. I have tried the following code to perform a right click of the mouse:

robot.mousePress(InputEvent.BUTTON3_MASK);
robot.mouseRelease(InputEvent.BUTTON3_MASK);

And it worked.

I'm testing it on a client side 3d online game.

Pressing the key "1" should perform some kind of a movement ingame, and when I tried the following code it didn't worked:

robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);

But it did worked when I used that code while speaking in the chat in game.

It's been tested over and over and I keep getting the same result.

Is it something that I have done wrong?or somehow the game detect that I'm not the one that pressing that key.

Upvotes: 0

Views: 768

Answers (1)

nneonneo
nneonneo

Reputation: 179717

You are probably releasing the key too quickly. Try sleeping for 30~60ms before releasing the key:

robot.keyPress(KeyEvent.VK_1);
try {
    Thread.sleep(50);
} catch(Exception e) {
    e.printStackTrace();
}
robot.keyRelease(KeyEvent.VK_1);

Upvotes: 6

Related Questions