Robot

I have been trying to use the Robot class to automatically control a game, but it turns out it works everywhere except inside the game.

I tested a couple of games and the same thing keeps happening. Google wasn’t really helpful on that one, but anyone have any clue why it cant emulate key presses in games?

Here is my code in case you want to have a look. It simply starts a thread and presses the left arrow 10 times.


new Thread(new Runnable(){
			Robot robot = null;
			int i = 0;
			
			@Override
			public void run() {
				
				try {
					robot = new Robot();
				} catch (AWTException e) {
					e.printStackTrace();
				}
				
				while (!stop) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println("wed");
					robot.keyPress(KeyEvent.VK_LEFT);
					robot.keyRelease(KeyEvent.VK_LEFT);
					i++;
					if (i>10) {
						stop = true;
					}
					
				}
			}
			
		}).start();

Maybe ‘the game’ checks whether a key is pressed, instead of checking for up/down events.


  robot.keyPress(KeyEvent.VK_LEFT);
+ Thread.sleep(100);
  robot.keyRelease(KeyEvent.VK_LEFT);

The key press event is too fast.

robot.setAutoDelay(200);

I tryied adding some delay, even a big one, but still nothing. Maybe there is other way to press keys using script?

You sure you’re having the specified game/program as the current active window while you’re using the robot?

Yes. Check that. The menu ingame works normally with the key press event. Not the actual game though.

It is a private video.

Then the key press works fine. What is the keybind for “left” inside the game/emulator? It may not necessarily be, and probably isn’t, the left arrow key.

[edit]: Also there are better ways to do a bot for this. You could look at the source code of the dozens of emulators and simply simulate the key press events directly in the code and recompile.

Or you could alter the exact memory location of the INPUT register of the AGB and DS emulators. Both have the same location 0x130 (http://cdn.preterhuman.net/texts/gaming_and_diversion/Gameboy%20Advance%20Programming%20Manual%20v1.1.pdf). You’ll have to use native (JNI) functions to access and alter a process memory.

The left key makes the little white rectangle which is on letter K on the video to go left. It works fine using a keyboard.

I used it on my last game project, too and it doesn’t respond to robot events as well.

Works fine for me. Try binding left to another key, say ‘L’ and send the key press event for the letter ‘L’ instead.

I was about to say that. I tryied it and it worked. Thank you for your time.