【问题标题】:JavaFX HTMLEditor ignores enter on red hatJavaFX HTMLEditor 忽略红帽上的输入
【发布时间】:2018-07-20 18:05:02
【问题描述】:

我试图弄清楚如何绕过 JavaFX HTMLEditor 忽略的 enter 键。

操作系统:

 Redhat Enterprise Linux Workstation 7.5

Java:

java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)

回车键不起作用,但 Control-M 起作用。我试图通过拦截所有回车键来解决这个问题,然后用回车替换它们。但是,当我以编程方式触发回车键事件时,什么也没有发生。

我的测试代码是:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;

public class HTMLEditorTest extends Application {

    @Override
    public void start(Stage stage) {
        stage.setTitle("HTMLEditor Test");
        final HTMLEditor htmlEditor = new HTMLEditor();
        Scene scene = new Scene(htmlEditor, 800, 800);

        htmlEditor.addEventFilter(KeyEvent.KEY_PRESSED, event ->
        {
            //System.out.println(htmlEditor.getHtmlText());
            if (event.getCode() == KeyCode.M && event.isControlDown())
            {
                 System.out.println("\nSource: " + event.getSource().getClass().getName());
                 System.out.println("Target: " + event.getTarget().getClass().getName());
                 System.out.println("Character: '" + event.getCharacter().hashCode() + "'");
                 System.out.println("Text: '" + event.getText() + "'");
                 System.out.println("Text: '" + event.getText().hashCode() + "'");
                 System.out.println("Shift Down: " + event.isShiftDown());
                 System.out.println("Control Down: " + event.isControlDown());
                 System.out.println("Meta Down: " + event.isMetaDown());
                 System.out.println("Alt Down: " + event.isAltDown() + "\n");
             }

             if (event.getCode() == KeyCode.ENTER)
             {
                 KeyEvent ke = new KeyEvent(KeyEvent.KEY_PRESSED, "", "\r", KeyCode.M, false, true, false, false);
                 KeyEvent.fireEvent(event.getTarget(), ke);
             }

         });

         stage.setScene(scene);
         stage.show();
    }

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

如果你运行它并输入“Control-M”,<p><br></p> 会被插入到 htmlText 中。 如果您键入“Enter”,即使我正在触发一个应该触发与“Control-M”相同的反应的键事件,也不会发生任何事情。 为了调查这一点,我打印了关于“Control-M”生成的 KeyEvent 和我触发的 KeyEvent 的所有信息。据我所知,它们是相同的。

当我输入“Enter”时,拦截“Enter”键事件,然后触发一个新的键事件:

<html dir="ltr"><head></head><body contenteditable="true"></body></html>

Source: javafx.scene.web.HTMLEditor
Target: javafx.scene.web.WebView
Character: '0'
Text: '
'
Text: '13'
Shift Down: false
Control Down: true
Meta Down: false
Alt Down: false

当我输入“Control-M”时:

<html dir="ltr"><head></head><body contenteditable="true"><p><br></p></body></html>

Source: javafx.scene.web.HTMLEditor
Target: javafx.scene.web.WebView
Character: '0'
Text: '
'
Text: '13'
Shift Down: false
Control Down: true
Meta Down: false
Alt Down: false

它们看起来相同,但只有通过实际键入“Control-M”生成的 KeyEvent 才起作用。

有什么想法吗?

【问题讨论】:

    标签: javafx redhat html-editor


    【解决方案1】:

    这对我有用,让“Enter”键实际插入新行。似乎 HTMLEditorSkin 中的私有方法没有被正确调用。请注意,这不是一个很好的解决方案,我认为这是一个丑陋的黑客。

    htmlEditor.addEventFilter(KeyEvent.KEY_PRESSED, event ->
            {
                if (event.getCode() == KeyCode.ENTER)
                {
                    event.consume();
    
                    final HTMLEditorSkin skin = (HTMLEditorSkin) htmlEditor.getSkin();
    
                        try
                        {
                            // Invoke the private 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);
                        }
                }
            });
    

    【讨论】:

      猜你喜欢
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 2012-09-29
      相关资源
      最近更新 更多