KeyEvent 停止监听 JavaFX

KeyEvent stop Listening JavaFX

我有一个非常简单的 KeyEvent:

@Override
public void start(Stage stage) throws Exception {
    Group root = new Group();
    Scene scene = new Scene(root);
    Canvas canvas = new Canvas( 900, 700 );

    scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent ke) {

            switch (ke.getCode()) {
                case UP:
                    System.out.println("UP");
                    break;
                case DOWN:
                    System.out.println("DOWN");
                    break;
                case LEFT:
                    System.out.println("LEFT");
                    break;
                case RIGHT:
                    System.out.println("RIGHT");
                    break;
                case ENTER:
                    //ke.consume();
                    break;
                default:
                    break;
            }
        }});

我想达到的目标: 我开始 运行 程序,如果我按向上、向下、向左、向右,输出将向上、向下、向左、向右写回给我(正确没问题)。 但!如果我按下 enter,如果我按下上、下、左、右、回车或其他键,则不会写回输出 UP、DOWN、LEFT、RIGHT。

所以我喜欢在按下 ENTER 按钮时停止 KeyEvent "running"。

你有好几个机会,我最先想到这两个。

1) 将按键事件的侦听器存储为 class 的成员,然后按 ENTER 键取消注册。

private EventHandler<KeyEvent> keyHandler;


@Override
public void start(Stage stage) {
    final Scene scene = new Scene(new Group());
    stage.setTitle("Table View Sample");
    stage.setWidth(450);
    stage.setHeight(500);

    keyHandler = new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent ke) {

            switch (ke.getCode()) {
                case ENTER:
                    scene.removeEventHandler(KeyEvent.KEY_PRESSED, keyHandler);
                    break;
                case UP:
                    System.out.println("UP");
                    break;
                case DOWN:
                    System.out.println("DOWN");
                    break;
                case LEFT:
                    System.out.println("LEFT");
                    break;
                case RIGHT:
                    System.out.println("RIGHT");
                    break;


                default:
                    break;
            }
        }};

   scane.addEventHandler(KeyEvent.KEY_PRESSED, keyHandler);

2) 在按下 ENTER 时,将布尔值存储到成员中,如果设置了该成员,则使用该事件。

private Boolean enterPressed = false;

@Override
public void start(Stage stage) {
    final Scene scene = new Scene(new Group());
    stage.setTitle("Table View Sample");
    stage.setWidth(450);
    stage.setHeight(500);

   scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent ke) {

            if(enterPressed){
                ke.consume();
                return;
            }

            switch (ke.getCode()) {
                case ENTER:
                    enterPressed = true;
                    break;
                case UP:
                    System.out.println("UP");
                    break;
                case DOWN:
                    System.out.println("DOWN");
                    break;
                case LEFT:
                    System.out.println("LEFT");
                    break;
                case RIGHT:
                    System.out.println("RIGHT");
                    break;

                default:
                    break;
            }
        }});

两个都行,选你喜欢的。