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

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

问题描述

我需要检测小键盘按键才能做一些事情。这是代码:

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',虽然事件被消耗,但是charachter'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事件无法区分键盘按下(代码= 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如何防止键在keypressed事件中输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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