javafx: How to bind the Enter key to a button and fire off an event when it is clicked?

前端 未结 4 1431
忘掉有多难
忘掉有多难 2020-12-16 19:06

Basically, I have a okayButton that sits in a stage and when it is clicked , it performs a list of tasks. Now I want to bind the Enter

相关标签:
4条回答
  • 2020-12-16 19:42

    First, set a hanlder on your button :

    okayButton.setOnAction(e -> {       
           ......
    });
    

    If the button has the focus, pressing Enter will automatically call this handler. Otherwise, you can do this in your start method :

    @Override
    public void start(Stage primaryStage) {
          // ...
          Node root = ...;
          setGlobalEventHandler(root);
    
          Scene scene = new Scene(root, 0, 0);
          primaryStage.setScene(scene);
          primaryStage.show();
    }
    
    private void setGlobalEventHandler(Node root) {
        root.addEventHandler(KeyEvent.KEY_PRESSED, ev -> {
            if (ev.getCode() == KeyCode.ENTER) {
               okayButton.fire();
               ev.consume(); 
            }
        });
    }
    

    If you have only one button of this kind, you can use the following method instead.

    okayButton.setDefaultButton(true);
    
    0 讨论(0)
  • 2020-12-16 19:47

    You can dynamically change the default button property of the currently focused button by using binding

    btn.defaultButtonProperty().bind(btn.focusedProperty());
    
    0 讨论(0)
  • 2020-12-16 19:47

    I've had the same problem like mynameisJEFF. (I'm using Windows and as I read here: http://mail.openjdk.java.net/pipermail/openjfx-dev/2016-June/019234.html it is the SPACE_BAR and not ENTER, which fires a Button in JavaFX) I didn't want to add a listener to every Button, so I registered a Listener to the root node and asked the scene, which node is focused to fire that one. Here is my code (it is xtend, but I think it very easy to understand):

    override start(Stage primaryStage) throws Exception {
        val root = FXTable.createRoot
        val mainScene = new Scene(root)
        root.addEventHandler(KeyEvent.KEY_RELEASED, [event|
            if(event.code === KeyCode.ENTER){
                switch(focusedNode : mainScene.focusOwnerProperty.get){
                    Button:{
                        focusedNode.fire
                        event.consume
                    }
                    default:{
                    }
                }
            }
        ])
        primaryStage.scene = mainScene
        primaryStage.show
        primaryStage.maximized = true
    }
    
    0 讨论(0)
  • 2020-12-16 19:55

    There is a much more simple a standard way to do that using setOnKeyPressed

    okayButton.setOnKeyPressed(event -> {
            if (event.getCode().equals(KeyCode.ENTER)) {
                okayButton.fire();
            }
        }
        );
    

    And don't forget that you should define SetOnAction too, other way it's work but it's doing nothing.

    okayButton.setOnAction(event -> {
            // Do what ever you want to your button do. Like :
            System.Out.Print("Okay Button Fired (Clicked or Pressed");
        }
        );
    
    0 讨论(0)
提交回复
热议问题