javafx:如何将 Enter 键绑定到按钮并在单击时触发事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32038418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:57:39  来源:igfitidea点击:

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

javajavafx

提问by mynameisJEFF

Basically, I have a okayButtonthat sits in a stageand when it is clicked , it performs a list of tasks. Now I want to bind the Enterkey to this button such that when it is clicked OR the ENTER key is pressed, it performs a list of tasks.

基本上,我有一个okayButton位于 a 中stage,当它被点击时,它会执行一个任务列表。现在我想将Enter键绑定到这个按钮,这样当它被点击或按下 ENTER 键时,它会执行任务列表。

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

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

我怎样才能做到这一点 ?我已经阅读了以下帖子。然而,它并没有帮助我实现我想做的事情。

采纳答案by Dici

First, set a hanlder on your button :

首先,在您的按钮上设置一个处理程序:

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

If the button has the focus, pressing Enterwill automatically call this handler. Otherwise, you can do this in your startmethod :

如果按钮具有焦点,按Enter 键将自动调用此处理程序。否则,您可以在您的start方法中执行此操作:

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

回答by armin.miedl

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.htmlit 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):

我遇到了与 mynameisJEFF 相同的问题。(我正在使用 Windows,正如我在此处阅读的:http: //mail.openjdk.java.net/pipermail/openjfx-dev/2016-June/019234.html 它是 SPACE_BAR 而不是 ENTER,它会在JavaFX)我不想为每个按钮添加一个监听器,所以我在根节点注册了一个监听器并询问场景,哪个节点专注于触发那个。这是我的代码(它是 xtend,但我认为它很容易理解):

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
}

回答by Abdul.Moqueet

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

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

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

回答by ARiyou Jahan

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

使用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.

并且不要忘记您也应该定义 SetOnAction ,否则它可以工作但它什么也不做。

okayButton.setOnAction(event -> {
        // Do what ever you want to your button do. Like :
        System.Out.Print("Okay Button Fired (Clicked or Pressed");
    }
    );