I write a method to simulate key press from KeyEvent, like below:
private Robot robot(){
if(robot==null){
try {
return new Robot();
} catch (AWTException e) {
throw new RuntimeException("Failed to create instance of Robot");
}
}else{
return robot;
}
}
public void sendKeyEvent(KeyEvent evt) throws IOException {
int type = evt.getID();
if(type == KeyEvent.KEY_PRESSED){
if(evt.isShiftDown()){
robot().keyPress(KeyEvent.VK_SHIFT);
}
robot().keyPress(evt.getKeyChar());
}else if(type == KeyEvent.KEY_RELEASED){
robot().keyRelease(evt.getKeyChar());
if(evt.isShiftDown()){
robot().keyRelease(KeyEvent.VK_SHIFT);
}
}
}
When this method received press 'A' key event, it could type 'A'.
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='A',modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_UNKNOWN]]
But the problem is when it received this KeyEvent(press 'a'), it acturaly pressed "1".
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='a',keyLocation=KEY_LOCATION_UNKNOWN]]
Could anyone tell me what wrong with this method?