JavaFX KeyEvent和重音字符 [英] JavaFX KeyEvent and accented characters

查看:183
本文介绍了JavaFX KeyEvent和重音字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有可疑主体的WebView作为JavaFX中的富文本编辑器。它工作正常,但我需要听按键。它适用于输入键和ASCII字符,但重音字符(如冰岛á和é)不会触发任何事件。我尝试了KeyEvent.KEY_PRESSED和KeyEvent.KEY_TYPED,并且它们都没有触发重音字符。

I'm using a WebView with a contenteditable body as a rich text editor in JavaFX. It's working fine, but I need to listen for key presses. It works for the enter key and ASCII characters, but accented characters (like the icelandic á and é) don't fire any event. I tried KeyEvent.KEY_PRESSED and KeyEvent.KEY_TYPED, and neither of them fire for accented characters.

InputMethodEvent会触发重音字符,但如果我为此设置了一个监听器,它似乎自动消耗了这个事件而且它没有进入编辑器。

InputMethodEvent does fire for accented characters, but if I setup a listener for that, it seems to automatically consume the event and it doesn't get to the editor.

任何人都知道一种方法来监听事件,当输入重音字符时没有消耗它们或者在InputMethodEvent中消耗角色的方法?

Anybody know of a way to listen for event when accented characters are typed that doesn't consume them or of a way to unconsume the character in InputMethodEvent?

推荐答案

您可以将KeyEvent EventHandler添加到任何节点(在此使用 Node.addEventHandler 方法,如果您处理EventType KeyEvent.KEY_TYPED ,您可以使用<获取 unicode 字符a href =https://docs.oracle.com/javase/8/javafx/api/javafx/scene/input/ KeyEvent.html#getCharacter--rel =nofollow> KeyEvent.getCharacter 方法。看到这个例子:

You can add an KeyEvent EventHandler to any Node (in this case your WebView) with the Node.addEventHandler method, and if you handle the EventType KeyEvent.KEY_TYPED you can get the typed unicode characters with the KeyEvent.getCharacter method. See this example:

WebView myWebView = new WebView();
myWebView.addEventHandler(KeyEvent.KEY_TYPED,
                    new EventHandler<KeyEvent>()
                    {
                        @Override
                        public void handle(KeyEvent event)
                        {
                            System.out.println("Unicode character typed: "+event.getCharacter());

                            switch (event.getCharacter())
                            {
                            case "á":
                                System.out.println("Typed accented a");
                                break;
                            case "é":
                                System.out.println("Typed accented e");
                                break;
                            case "í":
                                System.out.println("Typed accented i");
                                break;
                            case "ó":
                                System.out.println("Typed accented o");
                                break;
                            case "ú":
                                System.out.println("Typed accented u");
                                break;
                            default:
                                System.out.println("Typed other key " + event.getCode());
                                break;
                            }
                        }

                    });

您可能需要查看 Collat​​or 类,如果你想比较不同的字符串,忽略语言环境,大写,小写等等。它可能是如果你想考虑á和a相等,那就很有用。

You might want to take a look at the Collator class if you want to compare different strings ignoring locale, uppercase, lowercase, etc. It might be useful if you want to consider "á" and "a" equal.

祝你好运!

这篇关于JavaFX KeyEvent和重音字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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