Skip to Main Content

Java SE (Java Platform, Standard Edition)

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!

How do you make Robot type out a variable String?

843807Jan 19 2010 — edited Jan 19 2010
I don't know why I'm having an issue with this. I want the Robot class to type out a string one character at a time as if I were sitting at the keyboard and doing it myself. There will be many, variable strings to type. Here is what I tried to do and it fails miserably:
private String myString = "example";

// Loop for each character in the string
for(int i=0; i<numChars; i++)
{
	// Get the current character
	myChar = myString.charAt(i);
	
	// Convert the character to a keycode
	myKC = (int)myChar;
	
	// Press the key, release the key
	myRobot.keyPress(myKC);
	myRobot.delay(10);
	myRobot.keyRelease(myKC);
}
When I run it, I get some unexpected output in the Notepad window I have open. Instead of it spelling out "example", it gives me something entirely different:

e - instead of 'e', it gives me a '5'
x - instead of 'x', it gives me nothing at all
a - instead of 'a', it gives me a '1'
m - instead of 'm', it gives me a '-'
p - instead of 'p', 'F1' is triggered and the Notepad help window pops open
l - instead of 'l', it gives me nothing at all
e - instead of 'e', it gives me another '5'

What am I doing wrong here? This was supposed to be easy...

Edited by: ConQuesimo on Jan 19, 2010 12:06 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 16 2010
Added on Jan 19 2010
2 comments
211 views