JavaFX TextField如何防止在按键事件中输入键 [英] JavaFX TextField how to prevent key from getting typed in keypressed event

查看:35
本文介绍了JavaFX TextField如何防止在按键事件中输入键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检测小键盘按键来做一些事情.这是代码:

I need to detect the numpad key presses to do some stuff. here is the code:

textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                if(event.getCode().isKeypadKey()) {
                    //do stuff here...
                    event.consume();
                }
            }
        });

假设用户在小键盘上按下2",虽然事件已被消耗,但字符2"将出现在文本字段中.但我不想要那个.任何想法如何防止这种情况?

lets say user presses '2' on the numpad, although the event is consumed, the charachter '2' will appear in the textfield. but i dont want that. Any ideas how to prevent this?

更有趣的是,如果我抛出一个虚拟异常而不是消费事件,它将阻止 keytyped 事件.

whats more interesting is that if i throw a dummy exception instead of consuming the event, it will prevent the keytyped event.

推荐答案

您遇到的问题可以通过添加日志监听器来解释:

The problem you are facing can be explained by adding a logging listener:

textField.addEventFilter(Event.ANY, e -> System.out.println(e));

所以当你输入一个键时,你会得到这个:

So when you type a key you get this:

KeyEvent [source = TextField@42924cd[styleClass=text-input text-field], target = TextField@42924cd[styleClass=text-input text-field], eventType = KEY_PRESSED, consumed = false, character = , text = 1, code = NUMPAD1]
KeyEvent [source = TextField@42924cd[styleClass=text-input text-field], target = TextField@42924cd[styleClass=text-input text-field], eventType = KEY_TYPED, consumed = false, character = 1, text = , code = UNDEFINED]
KeyEvent [source = TextField@42924cd[styleClass=text-input text-field], target = TextField@42924cd[styleClass=text-input text-field], eventType = KEY_RELEASED, consumed = false, character = , text = 1, code = NUMPAD1]

换句话说,KeyTyped 事件无法区分键盘按下 (code=UNDEFINED),因此您无法捕捉到它.

In other words the KeyTyped event can't distinguish the keypad press (code=UNDEFINED), so you can't catch it.

它与异常一起工作的原因与您只使用事件而不检查任何内容相同.您可以使用它并防止打字.但是,不能用键盘检查.

The reason why it works with an exception is the same as if you just consume the event without checking anything. You can consume it and prevent the typing. However, not with the keypad check.

但话又说回来,无论如何你都不应该阻止它.应该由用户决定他想使用他的键盘或任何输入机制.

But then again, you shouldn't prevent it anyway. It should be up to the user as to what he wants to use his keypad or whatever input mechanism for.

但如果你必须,你必须.鉴于该信息,此问题的解决方法可能是:

But if you must, you must. Given that information a workaround for this problem could be:

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        HBox root = new HBox();

        EventHandler<KeyEvent> handler = new EventHandler<KeyEvent>() {

            private boolean willConsume = false;

            @Override
            public void handle(KeyEvent event) {

                if (willConsume) {
                    event.consume();
                }

                if (event.getCode().isKeypadKey()) {
                    if (event.getEventType() == KeyEvent.KEY_PRESSED) {
                        willConsume = true;
                    } else if (event.getEventType() == KeyEvent.KEY_RELEASED) {
                        willConsume = false;
                    }
                }
            }

        };

        TextField textField = new TextField();
        textField.addEventFilter(KeyEvent.ANY, handler);

        // logging
        textField.addEventFilter(KeyEvent.ANY, e -> System.out.println(e));

        root.getChildren().addAll(textField);

        Scene scene = new Scene(root, 300, 100);

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

    }

}

这是简单的版本.实际上,您必须使用扩展机制来处理多个按键.

That's the simple version. Actually you'd have to use an extended mechanism in order to deal with multiple keypresses.

这篇关于JavaFX TextField如何防止在按键事件中输入键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆