问题
I would like to call this java function in another function. How can I manualy fire a KeyEvent?
private void chooseItemByKey(KeyEvent event)
I tried
chooseItemByKey(new KeyEvent(null, null, null, null, null, KeyCode.DOWN, false, false, false, false));
to fire a KeyCode "Down" but my JRE told me there's a
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
The method needs the KeyEvent because I trigger it also by a key, but I need to trigger the function as well from another function whit out hitting a key on my keyboard. Any ideas?
回答1:
Just refactor it so it calls a different method that takes just the KeyCode
:
@FXML
private void chooseItemByKey(KeyEvent event) {
chooseItemByKeyCode(event.getCode());
}
private void chooseItemByKeyCode(KeyCode code) {
// essentially whatever you previously had in chooseItemByKey...
}
Then you just need to call
chooseItemByKeyCode(KeyCode.DOWN);
来源:https://stackoverflow.com/questions/33263360/call-a-function-that-requires-a-keyevent-parameter