JavaFX HMTLEditor doesn't react on 'return' key

◇◆丶佛笑我妖孽 提交于 2019-12-22 09:30:01

问题


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);
        }
    });

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.


回答1:


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?

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;
    }
});

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.




回答2:


I found out that it's an already known bug in JavaFX.

https://javafx-jira.kenai.com/browse/RT-33354
and
http://javafx-jira.kenai.com/browse/RT-20887

But FYI, it was resolved as "Won't Fix" for JavaFX 2.2. There is no problem in JavaFX 8.




回答3:


I still see this problem with Oracle JDK 10. Peeking into the HTMLEditorSkin, there is a Command.INSERT_NEW_LINE, but it is not performed when pressing 'Enter'. In principle, there is API for executing a Command, and that could be invoked from a key event filter, but the API is private.

The following is a hack around this limitation. It "works", but it is of course a hack that might break with future updates of JavaFX:

HTMLEditor editor = /* .. somehow get the HTMLEditor .. */
editor.addEventFilter(KeyEvent.KEY_PRESSED, event ->
{
    if (event.getCode() == KeyCode.ENTER)
    {
        event.consume();
        final HTMLEditorSkin skin = (HTMLEditorSkin) htmlEditor.getSkin();
        try
        {
            // Use reflection to invoke the private method
            // executeCommand(Command.INSERT_NEW_LINE.getCommand(), null);
            final Method method = skin.getClass().getDeclaredMethod("executeCommand", String.class, String.class);
            method.setAccessible(true);
            method.invoke(skin, Command.INSERT_NEW_LINE.getCommand(), null);
        }
        catch (Throwable ex)
        {
            throw new RuntimeException("Cannot hack around ENTER", ex);
        }
    }
});



回答4:


you can generate a line feed by filtering events and firing a <^m> events to the webview instead

        // fix enter ignored on linux
    HTMLEditor editor = ...
    WebView editorView = (WebView) editor.lookup(".web-view");
    editor.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
        if (e.getCode() == KeyCode.ENTER) {
            e.consume();
            editorView.fireEvent(new KeyEvent(
                    e.getSource(),
                    editorView,
                    KeyEvent.KEY_TYPED,
                    "\r", "", KeyCode.ENTER,
                    false, true, false, false));
        }
    });


来源:https://stackoverflow.com/questions/11269632/javafx-hmtleditor-doesnt-react-on-return-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!