在JavaFX场景上多按键

时间:2014-04-14 05:00:23

标签: java javafx

如何在单个场景中检测到多个关键事件?我需要我的程序来检测何时同时按下空格键右箭头键。

scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
    public void handle(KeyEvent ke) {
        if (ke.getCode() == KeyCode.RIGHT) {
            ///
        }

        if (ke.getCode() == KeyCode.LEFT) {
            ///
        }

        if (ke.getCode() == KeyCode.SPACE) {
            ///
        }

        if (ke.getCode() == KeyCode.RIGHT && ke.getCode() == KeyCode.SPACE) {
            // How??
        }
    }
});

前3个表达式查找单个键并且正常工作。最后的表达式永远不会返回true。我相信只有最新的关键事件才会传递给处理程序。

我查看了KeyCodeCombination,但这似乎是在密钥具有指定列表中的修饰键(ALT_DOWN,SHIFT_DOWN等)的情况下使用。

我可以使用FX中的实用程序吗?

2 个答案:

答案 0 :(得分:10)

试试这个:

final BooleanProperty spacePressed = new SimpleBooleanProperty(false);
final BooleanProperty rightPressed = new SimpleBooleanProperty(false);
final BooleanBinding spaceAndRightPressed = spacePressed.and(rightPressed);

// How to respond to both keys pressed together:
spaceAndRightPressed.addListener(new ChangeListener<Boolean>() {
    @Override
    public void changed(ObservableValue<Boolean> obs, Boolean werePressed, Boolean arePressed) {
        System.out.println("Space and right pressed together");
    }
});

// Wire up properties to key events:
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent ke) {
        if (ke.getCode() == KeyCode.SPACE) {
            spacePressed.set(true);
        } else if (ke.getCode() == KeyCode.RIGHT) {
            rightPressed.set(true);
        }
    }
});

scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent ke) {
        if (ke.getCode() == KeyCode.SPACE) {
            spacePressed.set(false);
        } else if (ke.getCode() == KeyCode.RIGHT) {
            rightPressed.set(false);
        }
    }
});

答案 1 :(得分:2)

使用热键关闭应用程序 mac:command + w / esc
windows:alt + F4 / esc

    public class JavaApp {/*inner: public class YourApplication extends Application*/
        public void exit() {
            stage.close();
            System.exit(0);
        }
    }

        KeyCombination keyCombinationMac = new KeyCodeCombination(KeyCode.W, KeyCombination.SHORTCUT_DOWN);
        KeyCombination keyCombinationWin = new KeyCodeCombination(KeyCode.F4, KeyCombination.ALT_DOWN);
        scene.addEventHandler(KeyEvent.KEY_RELEASED, event -> {
            if (keyCombinationMac.match(event)
                    || keyCombinationWin.match(event)
                    || event.getCode() == KeyCode.ESCAPE
                    ) {
                new JavaApp().exit();
            }
        });

来自:https://dzone.com/articles/handling-keyboard-sortcuts

final KeyCombination keyComb1 = new KeyCodeCombination(KeyCode.R, KeyCombination.CONTROL_DOWN);
scene.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler() {
                @Override
                public void handle(KeyEvent event) {
                    if (keyComb1.match(event)) {
                        System.out.println("Ctrl+R pressed");
                    }
                }
            });


scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
 public void handle(final KeyEvent keyEvent) {
   if (keyEvent.getCode() == KeyCode.F5) {
    System.out.println("F5 pressed");
    //Stop letting it do anything else
    keyEvent.consume();
   }
 }
});