简体   繁体   English

如果按下键,则不会触发用于鼠标交互的JavaFX事件

[英]JavaFX Events for Mouse Interactions are not triggered if Key is pressed

JavaFX does not execute events like the ActionEvent for Button or CheckBox , if a modifier key like CTRL or SHIFT is pressed. 如果按下CTRL或SHIFT等修饰键,则JavaFX不会执行ActionEvent for ButtonCheckBox等事件。 As far as I understand this behavior is implemented in ButtonBehavior (eg note the expression ! keyDown in the following method from that class): 据我所知,这种行为是在ButtonBehavior实现的(例如,请注意该类中以下方法中的表达式! keyDown ):

@Override public void mouseReleased(MouseEvent e) {
    // if armed by a mouse press instead of key press, then fire!
    final ButtonBase button = getControl();
    if (! keyDown && button.isArmed()) {
        button.fire();
        button.disarm();
    }
}

First of all, I do not really understand the reason for this. 首先,我真的不明白这个的原因。 What is the purpose of not firing a button if a key is pressed? 如果按下某个键,不触发按钮的目的是什么?

This is my use-case: I want to implement a checkbox that can be checked/unchecked as normal. 这是我的用例:我想实现一个可以正常检查/取消选中的复选框。 It will toggle some state in a model. 它将切换模型中的某些状态。 But it should have an additional feature: if the user presses some key like CTRL while checking/unchecking with the mouse, an additional flag called "locked" or "protected" should be set in the model, which will prevent that the state can be overwritten by some other logic of the application. 但它应该有一个额外的功能:如果用户在用鼠标检查/取消选中时按下某些键(如CTRL),则应在模型中设置一个名为“locked”或“protected”的附加标志,这将阻止状态为被应用程序的其他一些逻辑覆盖。

This should give an idea about the use-case, but if not it doesn't really matter for my actual question: How can I make it possible that a CheckBox can still be toggled (or a Button be pressed) even though the user presses a modifier key? 这应该给出一个关于用例的想法,但如果没有,它对我的​​实际问题并不重要:即使用户按下,我怎样才能使CheckBox仍然可以切换(或按下Button )一个修饰键?

Thanks for your help! 谢谢你的帮助!

That is odd you can implement it yourself like so 这很奇怪,你可以自己实现它

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        VBox vBox = new VBox();
        vBox.setAlignment(Pos.CENTER);

        CheckBox checkBox = new CheckBox();
        checkBox.setOnMouseClicked(event -> {
            if(event.isControlDown()) {
                System.out.print("Control down click ");
                checkBox.setSelected(!checkBox.isSelected());
            }
            else
                System.out.print("Normal click ");

            System.out.println("Checked Status:"+checkBox.isSelected());
        });

        Button button = new Button("Button");
        button.setOnMouseClicked(event -> {
            if(event.isControlDown())
                System.out.println("Control down click");
            else
                System.out.println("Normal click");
        });

        vBox.getChildren().addAll(new Label("Click the box"),checkBox,button);


        primaryStage.setScene(new Scene(vBox));
        primaryStage.show();
    }

    public static void main(String[] args) { launch(args); }

}

The output for CheckBox: CheckBox的输出:

Normal click Checked Status:true
Normal click Checked Status:false
Control down click Checked Status:true
Control down click Checked Status:false

The output for Button: Button的输出:

Normal click
Control down click

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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