JavaFX关键监听器为多个键按下实现? [英] JavaFX key listener for multiple keys pressed implementation?
本文介绍了JavaFX关键监听器为多个键按下实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个事件处理程序来侦听多个键组合,例如同时按住 Ctrl 和 C 。
I would like to create an event handler that listens for multiple key combinations such as holding Ctrl and C at the same time.
为什么不能像那样((... ==控制)&&(... == C))
工作?
以下是我尝试使用的代码:
Here is the code I trying to work with:
textField.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
public void handle(KeyEvent event) {
if ((event.getCode() == KeyCode.CONTROL) && (event.getCode() == KeyCode.C)) {
System.out.println("Control pressed");
}
};
});
推荐答案
解决此问题的一种方法是创建 KeyCombination
对象并将其部分属性设置为您在下面看到的内容。
One way to tackle this problem is to create a KeyCombination
object and set some of its properties to what you see below.
请尝试以下操作:
textfield.getScene().getAccelerators().put(new KeyCodeCombination(
KeyCode.C, KeyCombination.CONTROL_ANY), new Runnable() {
@Override public void run() {
//Insert conditions here
textfield.requestFocus();
}
});
这篇关于JavaFX关键监听器为多个键按下实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文