rockZ
rockZ

Reputation: 875

Call a function that requires a KeyEvent parameter

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?

Upvotes: 0

Views: 1240

Answers (1)

James_D
James_D

Reputation: 209408

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);

Upvotes: 1

Related Questions