• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

is this possible?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you use java to send keyboard input to another application? for example, write a java program that could open notepad, "type" text, save, then close notepad? what i'm trying to do is make a bot/macro/whatever-its-called to play a gameboy emulator. my friend and i thought it would be cool to see if/how we could write something like this.
if this cant be done with java in anyway (i feel thats likely) then does anyone know any means of doing this? thanks for any help!
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at java.awt.Robot. It can put key and mouse events into the native event queue.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey this is something i wrote for one of our project... i know its not good to give code away... but couldn't resist as i think its a similar problem...

i hope the this still works... but it helped me in the coding the actual requirement...

just a few points to remember - keyPress will press the key so you might have to release it again by keyRelease...

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;

public class RobotApp {

public static void main(String[] args) {
try {

Process p = Runtime.getRuntime().exec("notepad");

Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_L);
robot.keyRelease(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_R);
robot.keyPress(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_D);

robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_CONTROL);

robot.keyPress(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_B);
robot.keyPress(KeyEvent.VK_C);
robot.keyPress(KeyEvent.VK_DECIMAL);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_ENTER);

robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F4);
robot.keyRelease(KeyEvent.VK_ALT);

} catch (AWTException awte) {
// do something
} catch (IOException ioe) {
// do something
}
}
}
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
by using UseCodeTags it would help to make this more readable too.
 
Micah Pezdirtz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow! thanks! im having a little trouble closing the application though, the alt+f4 part doesnt want to work for me, the program just stops and says "interrupted!". could you help me out?

edit: sorry, i fixed it by replacing the alt+f4 part with a p.destroy();
[ April 27, 2007: Message edited by: Micah Pezdirtz ]
 
Micah Pezdirtz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, sorry to bother you guys again, but i ran into something(but it might not be javas fault)
after my program starts the application, an emulator, and loads the gamefile, i tell it to start pressing up, or

however, this does notmake anything really happen. i know its getting the key "pressed", but the emulator isnt receiving the input. VK_UP is definitely the key that is used by the emulator, so why cant the emulator get the input correctly? anyone know?
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic