Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Robots problem

 
Himanshu Rawat
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

hope this is the right forum to ask this Q.

I made a simple java robot program, which opens a notepad and writes. After that its supposed to exit.

Although exit is not working but once i close this program, keys are working in arbitrary manner. for e.g. on pressing e, it clicks on tabs etc. I also need to reboot OS to get keys normal behavior back.


the code is:
public class RobotExp {

public static void main(String[] args) throws IOException {

try {

Robot robot = new Robot();
// Creates the delay of 5 sec so that you can open notepad before
// Robot start writting
// Runtime.getRuntime().exec("C:\\Program Files\\Internet Explorer\\iexplore.exe \"http://www.google.com\"");
Runtime.getRuntime().exec("notepad");
robot.delay(5000);

// robot.keyPress(KeyEvent.VK_CONTROL);
//robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_C);
robot.keyPress(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_D);
robot.keyPress(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_N);
robot.keyPress(KeyEvent.VK_ENTER);
robot.delay(5000);

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

// Don't save
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.delay(500);

robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(500);


} catch (AWTException e) {
e.printStackTrace();
}
}
}



Please tell me how to solve this problem?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know, but it seems odd that you are calling an exec() method without clearing the two associated streams. Google for "when Runtime.exec() won't" and read Michael Daconta's article before doing anything else.
 
Himanshu Rawat
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
solved . and even closing of notepad working fine too

thanks Campbell
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

How did you solve it? Did you use Daconta's hints?
 
Himanshu Rawat
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Campbell,

I forgot to post the solution.sorry for that.

I killed the process in the end and for closing notepad i was pressing Alt F4, i wasn't releasing them in the end so i released them.


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


i guess killing process was important . Silly mistakes

Before using Daconta's hints, i wanted to try myself and it worked before that. anyways i will still look into the hints

 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu Rawat wrote:Before using Daconta's hints, i wanted to try myself and it worked before that.

Then you got a bit lucky. I had quite a big application once that had one annoying bug - a conversion process would sometimes hang. I had no idea why. Then I read that article, applied it to the application, and the bug was gone.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic