我想让我的JFX应用程序模拟箭头键按下(当它们在TextField中注册时),但我无法弄清楚如何发送除字符串或字节之外的任何内容。
我想象这样的事情:
static EventHandler<KeyEvent> KEY() {
E = new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent ke) {
if (ke.getCode().equals(KeyCode.UP)) {
try {
//someObject.SimulateKeyPress(KeyCode.UP);
//OR
//coolObject.SendKey((char)KEY_UPKEY));
} catch (Exception ex) {
//Teleport goats
}
}
}
};
return E;
}
答案 0 :(得分:9)
使用课程Robot
try {
Robot r = new Robot();
//there are other methods such as positioning mouse and mouseclicks etc.
r.keyPress(java.awt.event.KeyEvent.VK_UP);
r.keyRelease(java.awt.event.KeyEvent.VK_UP);
} catch (AWTException e) {
//Teleport penguins
}
答案 1 :(得分:1)
您无法实例化Robot类型。 你应该做:
Robot robot = com.sun.glass.ui.Application.GetApplication().createRobot();