Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Robot  (Read 357 times)
0 Members and 1 Guest are viewing this topic.
Offline Pauler
« Posted 2014-03-04 20:11:08 »

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.


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
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();

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2014-03-04 20:15:42 »

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

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline jonjava
« Reply #2 - Posted 2014-03-04 20:18:06 »

The key press event is too fast.

1  
robot.setAutoDelay(200);



Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Pauler
« Reply #3 - Posted 2014-03-04 20:19:49 »

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

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


I tryied adding some delay, even a big one, but still nothing. Maybe there is other way to press keys using script?
Offline jonjava
« Reply #4 - Posted 2014-03-04 20:27:37 »

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

Offline Pauler
« Reply #5 - Posted 2014-03-04 20:34:25 »

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.
Offline Drenius
« Reply #6 - Posted 2014-03-04 20:36:12 »

It is a private video.
Offline jonjava
« Reply #7 - Posted 2014-03-04 20:39:34 »

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.

Offline Pauler
« Reply #8 - Posted 2014-03-04 20:41:33 »

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.
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.
Offline jonjava
« Reply #9 - Posted 2014-03-04 20:53:17 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Pauler
« Reply #10 - Posted 2014-03-04 20:54:16 »

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.
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (17 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (67 views)
2014-08-11 02:49:23

BurntPizza (45 views)
2014-08-09 21:09:32

BurntPizza (36 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!