简体   繁体   中英

Intercept Paste Event on JavaFx HTMLEditor

您好我想知道如何拦截JavaFX HTMLEditor中粘贴事件

You can't. The HTMLEditor uses a WebPage internally. Basically during a paste event it sends a "paste" command via

private boolean executeCommand(String command, String value) {
    return webPage.executeCommand(command, value);
}

and then a

twkExecuteCommand(getPage(), command, value);

However, you could intercept everything that implicitly invokes the paste event like the button click or a CTRL+V key combination and depending on what you wish to do consume the event.

Example:

public class HTMLEditorSample extends Application {

    @Override
    public void start(Stage stage) {

        final HTMLEditor htmlEditor = new HTMLEditor();

        Scene scene = new Scene(htmlEditor, 800, 600);
        stage.setScene(scene);
        stage.show();

        Button button = (Button) htmlEditor.lookup(".html-editor-paste");
        button.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
            System.out.println("paste pressed");
            // e.consume();
        });

        htmlEditor.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
            if( e.isControlDown() && e.getCode() == KeyCode.V) {
                System.out.println( "CTRL+V pressed");
                // e.consume();
            }
        });

    }

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

As to your other question with pasting only plain text to the html editor, you could do it like this:

public class HTMLEditorSample extends Application {

    @Override
    public void start(Stage stage) {

        final HTMLEditor htmlEditor = new HTMLEditor();

        Scene scene = new Scene(htmlEditor, 800, 600);
        stage.setScene(scene);
        stage.show();

        Button button = (Button) htmlEditor.lookup(".html-editor-paste");
        button.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
            modifyClipboard();
        });

        htmlEditor.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
            if( e.isControlDown() && e.getCode() == KeyCode.V) {
                modifyClipboard();
            }
        });

    }

    private void modifyClipboard() {
        Clipboard clipboard = Clipboard.getSystemClipboard();

        String plainText = clipboard.getString();
        ClipboardContent content = new ClipboardContent();
        content.putString(plainText);

        clipboard.setContent(content);
    }

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

It's a workaround and ugly because you should never modify the clipboard content unless the user wants to, but it works. On the other hand, it may be possible to revert the clipboard content back to its original state after the paste operation.

Edit:

Here's how you could access the contextmenu, eg for disabling it:

WebView webView = (WebView) htmlEditor.lookup(".web-view");
webView.setContextMenuEnabled(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM