简体   繁体   中英

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

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 key to this button such that when it is clicked OR the ENTER key is pressed , it performs a list of tasks.

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

How can I do that ? I have read the following post already. However, it did not help me to achieve what I want to do.

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);

您可以使用绑定动态更改当前聚焦按钮的默认按钮属性

btn.defaultButtonProperty().bind(btn.focusedProperty());

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
}

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");
    }
    );

This should work:

okayButton.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            if(evt.getKeyCode() == KeyEvent.VK_ENTER){
               System.out.print("Your function call or code can go here");
            }
        }
    });  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM