• Post Reply Bookmark Topic Watch Topic
  • New Topic

controlling an application by sending keystrokes from Java  RSS feed

 
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to control an application running in Windows 7 by sending keystroke shortcuts to that application.
The following code illustrates my problem using Microsoft Word. It launches the word application and successfully writes the "x" into the document. However, when I click on the Java JButton nothing happens. How can I fix this?
Thanks very much for any suggestions.
Joe
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if that helps, but to simulate a physical keypress, you need to call robot.keyRelease(//same key code); after calling keyPress. Otherwise the first key pressed is still down and might be conflicting with other keyPress events.
 
Joe Nikki
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion. Unfortunately, it didn't solve my problem but while experimenting with the program I discovered what is going on:
until I click the JButton, the Word application is the active window and keyboard output goes there. When I click the JButton the Java GUI becomes the active window and keyboard output no longer goes to the Word application. If I put a long delay in the JButton action-performed before sending the key strokes and use that time to manually activate the Word application (by clicking on it) subsequent keystrokes are entered into Word.
The question becomes then, how do I pass the focus back onto the Word application programmatically from Java?
(I have looked at many Java Robot exampleson the Internet but none of them have a Java GUI. Perhaps the Robot class is not designed to do this?)
Here is my revised program:

 
Ranch Hand
Posts: 242
14
Android Angular Framework AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you may have hit the wall with keystrokes. You are going to be trapped in there. Instead, have you considered simulating mouse clicks?

http://www.java2s.com/Code/Java/Development-Class/Simulateamouseclick.htm

The link shows a very simplistic example (no pointer location). There is a Robot.mouseMove method, as well. Unfortunately, you will be opening that can-of-worms: knowing what parts of the screen belong to what. Maybe you can compute something based on the location of the input field you are using?
 
Joe Nikki
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works great! (Found lots of other tutorials with more extensive use of the robot mouse). Now, if I can program/initialize my application to appear in the same screen location every time, I can set up my Java GUI to share my screen nicely with the application and I'm home free.
Many thanks.
In the meantime, I still haven't given up figuring out a way to transfer focus to the application without using robot mouse clicks. This would be "cleaner" as it would not depend on knowing the location of the application on the screen. But if I can't do that, the above solution should work very well.
Regards,
Joe
 
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried sending an alt-tab to change the focus back to the last window? That wouldn't depend on window location.
 
Joe Nikki
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried that and it works fine. There is a slight aesthetics problem in that the screen very briefly flashes what looks like an array of large icons (it happens so fast that I I'm not able to see what it is) before transferring focus to the previous window. It's always good to have more than one way to do something in case I run into problems using the absolute screen location mouse click. Thanks for the suggestion.
Regards,
Joe
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm impressed that the alt-tab idea worked; I wouldn't have predicted that.

So I'm awarding you a cow for posting an interesting question and following up on the responses.
 
Joe Nikki
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, thank you kindly, Sheriff. Is this a riding cow or something I need to cut up and cook? (I don't normally eat a lot of red meat).
Joe
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Me too in the red-meat-eating category, but no, no cows are harmed in the making of this forum! Here's our page explaining cows: http://www.coderanch.com/t/659926/Wiki/Ranch-Cows
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!