本文介绍了调用需要KeyEvent参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在另一个函数中调用这个java函数。我如何手动触发KeyEvent?

I would like to call this java function in another function. How can I manualy fire a KeyEvent?

    private void chooseItemByKey(KeyEvent event)

我试过

        chooseItemByKey(new KeyEvent(null, null, null, null, null, KeyCode.DOWN, false, false, false, false));

解雇KeyCodeDown但我的JRE告诉我有一个

to fire a KeyCode "Down" but my JRE told me there's a

线程中的异常JavaFX Application Threadjava.lang.IllegalArgumentException:参数类型不匹配

该方法需要KeyEvent,因为我也是通过一个键来触发它,但是我需要从另一个函数触发该函数,而不是按键盘上的键。
有什么想法吗?

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?

推荐答案

只需重构它就可以调用另一种只需 KeyCode :

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...
}

然后你只需要打电话

chooseItemByKeyCode(KeyCode.DOWN);

这篇关于调用需要KeyEvent参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 03:12