When I add an onKeyPressed event handler that consumes all keys, and a EventType.ROOT handler that monitors events, this 2nd handler still sees KEY_PRESSED events which are consumed.  Note the 3rd line in the output where a consumed = true event is being delivered.

Output:

KeyPressedHandler KeyEvent [source = javafx.scene.Scene@36dbf594, target = Button@5dd3d27e[styleClass=button]'111', eventType = KEY_PRESSED, consumed = false, character =  , text = f, code = F] Main KeyEvent [source = javafx.scene.Scene@36dbf594, target = Button@5dd3d27e[styleClass=button]'111', eventType = KEY_PRESSED, consumed = false, character =  , text = f, code = F] RootHandler KeyEvent [source = javafx.scene.Scene@36dbf594, target = Button@5dd3d27e[styleClass=button]'111', eventType = KEY_PRESSED, consumed = true, character =  , text = f, code = F] RootHandler KeyEvent [source = javafx.scene.Scene@36dbf594, target = Button@5dd3d27e[styleClass=button]'111', eventType = KEY_TYPED, consumed = false, character = f, text = , code = UNDEFINED] RootHandler KeyEvent [source = javafx.scene.Scene@36dbf594, target = Button@5dd3d27e[styleClass=button]'111', eventType = KEY_RELEASED, consumed = false, character =  , text = f, code = F]

Code:

    public static class App extends Application {
      @Override
      public void start(Stage primaryStage) {
        Button button1 = new Button("111");

        HBox hBox = new HBox();
        hBox.getChildren().addAll(button1);
        Scene scene = new Scene(hBox, 300, 300);

        scene.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
          System.out.println("KeyPressedHandler " + e);
        });
        scene.addEventHandler(EventType.ROOT, e -> {
          if(e instanceof KeyEvent) {
            System.out.println("RootHandler " + e);
          }
        });
        scene.setOnKeyPressed(e -> {
          System.out.println("Main " + e);
          e.consume();
        });

        primaryStage.setScene(scene);
        primaryStage.show();
      }

      /**
       * @param args the command line arguments
       */
      public static void start(String[] args) {
        App.launch(args);
      }
    }

--John

Reply via email to