JavaFX HMTLEditor对'return'键没有反应 [英] JavaFX HMTLEditor doesn't react on 'return' key

查看:143
本文介绍了JavaFX HMTLEditor对'return'键没有反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaFX' HTMLEditor 组件进行一些实验。我使用了以下代码(摘录):

I was trying to do some experiments with JavaFX' HTMLEditor component. I used the following code(excerpt):

    fxPanel=new JFXPanel();
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            Group group = new Group();
            scene = new Scene(group);               
            fxPanel.setScene(scene);
            view = VBoxBuilder.create().build();

            group.getChildren().add(view);


            edit = HTMLEditorBuilder.create().build();
           // toolPane = TabPaneBuilder.create().minHeight(60d).build();
            //toolPane.getTabs().add(new Tab("Allgemein"));

            view.getChildren().add(edit);

        }
    });

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            jPanel1.add(fxPanel);
        }
    });

到目前为止它运行正常,有一个重要的例外 - 我无法使用BR的返回键 - 似乎只是被忽略了。这根钥匙根本没有反应。据我所见,任何其他键都按预期工作。

It works fine so far with one important exception - i can't use the return key for a BR - it seems just to be ignored. There is no reaction on this key at all. As far as i could see, any other key works as expected.

推荐答案

我注意到 CTRL - M 适用于 Enter 的情况。所以我只是在JFXPanel上放一个 KeyListener ,将 KeyChar 从10更改为13并重新发布事件到系统事件队列。如果HTMLEditor开始响应 ENTER CTRL - M ,这可能会在以后停止工作。

I noticed that CTRL-M works where Enter doesn't. So I just worked around this by putting a KeyListener on the JFXPanel, changing the KeyChar from 10 to 13 and reposting the event to the System Event Queue. This may stop working as intended later on if the HTMLEditor starts responding to both ENTER and CTRL-M though.

fxPanel.addKeyListener(new KeyListener() {

    public void keyTyped(KeyEvent e) {
        if (e.getKeyChar() == 10) {
            e.setKeyChar((char) 13);
            Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(e);
        }
    }

    public void keyPressed(KeyEvent e) {}

    public void keyReleased(KeyEvent e) {}
});

现在任何人都有更好的主意吗?

Anyone have a better idea for now?

编辑:我发现另一种获得所需效果的方法是在当前键盘焦点管理器上安装自定义 KeyEventDispatcher ,如下所示:

Edit: I found another way to get the desired effect is to install a custom KeyEventDispatcher on the current keyboard focus manager like so:

KeyboardFocusManager kfm = DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager();
kfm.addKeyEventDispatcher(new KeyEventDispatcher() {
    @Override
    public boolean dispatchKeyEvent(KeyEvent e) {
        if (DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == jfxPanel) {
            if (e.getID() == KeyEvent.KEY_TYPED && e.getKeyChar() == 10) {
                e.setKeyChar((char) 13);
            }
        }
        return false;
    }
});

这样做的好处是可以更改原来的 KeyEvent 而不是之后发布一个新的,所以如果 HTMLEditor 开始响应 Enter 事件,我们就不会加倍。

This has the advantage of changing the original KeyEvent rather than posting a new one afterwards, so that if HTMLEditor were to start responding to Enter events we wouldn't be doubling up.

这篇关于JavaFX HMTLEditor对'return'键没有反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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