Call a function that requires a KeyEvent parameter

老子叫甜甜 提交于 2020-01-06 21:59:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!