How to programmatically simulate arrow key pressing in Java FX
•
Java
I want my JFX application to simulate arrow key presses (when they are registered in textfield), but I can't figure out how to send anything except strings or bytes
I imagine this:
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;
}
Solution
Using class 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
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
