JavaFX关键监听器为多个键按下实现?

我想创建一个事件处理程序来侦听多个键组合,例如同时按住CtrlC.

为什么if((... == Control) && (... == C))不起作用?

这是我尝试使用的代码:

 textField.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler() { public void handle(KeyEvent event) { if ((event.getCode() == KeyCode.CONTROL) && (event.getCode() == KeyCode.C)) { System.out.println("Control pressed"); } }; }); 

解决此问题的一种方法是创建KeyCombination对象并将其某些属性设置为您在下面看到的内容。

请尝试以下方法:

 textfield.getScene().getAccelerators().put(new KeyCodeCombination( KeyCode.C, KeyCombination.CONTROL_ANY), new Runnable() { @Override public void run() { //Insert conditions here textfield.requestFocus(); } }); 

你可以尝试这个解决方案,它对我有用!

 final KeyCombination keyCombinationShiftC = new KeyCodeCombination( KeyCode.C, KeyCombination.CONTROL_DOWN); textField.setOnKeyPressed(new EventHandler() { @Override public void handle(KeyEvent event) { if (keyCombinationShiftC.match(event)) { logger.info("CTRL + C Pressed"); } } }); 

这会有所帮助。 KeyCombination。

 final KeyCombination keyComb1=new KeyCodeCombination(KeyCode.C,KeyCombination.CONTROL_DOWN); 

https://code.google.com/p/javafx-demos/source/browse/trunk/javafx-demos/src/main/java/com/ezest/javafx/demogallery/KeyCombinationDemo.java?r=27