我想创建一个事件处理程序来侦听多个键组合,例如hold Ctrl和C 同时.
为什么不if((... == Control) && (... == C))
工作?
这是我尝试使用的代码:
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");
}
};
});
Run Code Online (Sandbox Code Playgroud)
你可以尝试这个解决方案,它对我有用!
final KeyCombination keyCombinationShiftC = new KeyCodeCombination(
KeyCode.C, KeyCombination.CONTROL_DOWN);
textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (keyCombinationShiftC.match(event)) {
logger.info("CTRL + C Pressed");
}
}
});
Run Code Online (Sandbox Code Playgroud)
解决此问题的一种方法是创建一个KeyCombination
对象并将其某些属性设置为您在下面看到的内容.
请尝试以下方法:
textfield.getScene().getAccelerators().put(new KeyCodeCombination(
KeyCode.C, KeyCombination.CONTROL_ANY), new Runnable() {
@Override public void run() {
//Insert conditions here
textfield.requestFocus();
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6973 次 |
最近记录: |