I'm attempting in making the Robot typing method a lot simpler.
Most of the keycodes in KeyEvent
are hex codes.
Plan:
String
to char[]
char[]
(int)char
(int)char
)(int)char
)What I have so far:
import java.awt.*;
import java.awt.event.*;
public class Driver
{
private static Robot r;
public static void send(String phrase)
{
char[] chars = phrase.toCharArray();
for (char letter:chars)
{
//int hex = hex value of (int)letter
//r.keyPress(hex);
//r.keyRelease(hex);
}
}
public static void main(String[]args)
{
try
{
r = new Robot();
r.delay(5000);
send("Hello World");
}
catch(AWTException e)
{
//Nothing
}
}
}
Keycodes for ASCII letters are equals to their uppercase char value. So you could do the following:
char[] chars = phrase.toUpperCase().toCharArray();
for (char letter:chars)
{
int keyCode = (int)letter;
r.keyPress(keyCode);
r.keyRelease(keyCode);
}
With this loop, the string "Hello World" will give "hello world". It won't work for things like exclamation points.
Furthermore, if you wan't the robot to send uppercase letters you will have to simulate a press on the shift key or on the caps lock key.
Not sure this method is reliable though. You may as well do a lot a if/else (or a switch) to return the correct key code constant from java.awt.event.KeyEvent.