Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need help with Robot class in writing sentences.

807601Feb 16 2008 — edited Feb 17 2008
I want to write a method that will click on a coordinate on my screen and type a sentence that I parse it. This method uses the Robot class of the Java API to simulate the mouse and keyboard. Currently the method works if I pass a sentence containing only the letters a-z or A-Z however if I parse it any other characters the compiler throws this exception:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid key code
	at sun.awt.windows.WRobotPeer.keyPress(Native Method)
	at java.awt.Robot.keyPress(Robot.java:224)
	at StringWriter.write(StringWriter.java:18)
	at PerfectCommander.main(PerfectCommander.java:11)
Here is the mothod that I have built thus far:
import java.awt.*;
import java.awt.event.*; 
import javax.swing.*; 

public class StringWriter {
	public void write(String sentence, int x, int y)throws AWTException { 
		Robot hand = new Robot();
		
		hand.delay(1000);
		
		// Move to location and left click
		hand.mouseMove(x,y);
		hand.mousePress(InputEvent.BUTTON1_MASK);
		hand.delay(300);
		hand.mouseRelease(InputEvent.BUTTON1_MASK);
		
		// Type the sentence through the simulation of keyboard keystrokes 
		for(int i = 0; i < sentence.length(); i++) {
			hand.keyPress(sentence.toUpperCase().charAt(i));
			hand.delay(300);
			hand.keyRelease(sentence.toUpperCase().charAt(i));
		}
		
	} 
}
Anybody have any ideas on how I can have the Robot class type entire sentences, regardless of content? Thank you for reading and I look forward to your help.

Edited by: larsendj on Feb 16, 2008 10:47 AM

Edited by: larsendj on Feb 16, 2008 10:49 AM

Edited by: larsendj on Feb 16, 2008 10:49 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 16 2008
Added on Feb 16 2008
3 comments
482 views