在 JavaFx HTMLEditor 上拦截粘贴事件
Intercept Paste Event on JavaFx HTMLEditor
你好我想知道如何在JavaFX HTMLEditor.
中拦截粘贴事件
你不能。 HTMLEditor 在内部使用 WebPage。基本上在粘贴事件期间,它通过
发送 "paste" 命令
private boolean executeCommand(String command, String value) {
return webPage.executeCommand(command, value);
}
然后是
twkExecuteCommand(getPage(), command, value);
但是,您可以拦截所有隐式调用粘贴事件的内容,例如按钮单击或 CTRL+V 组合键,具体取决于您希望使用该事件执行的操作。
示例:
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);
}
}
至于您的 other question 仅将纯文本粘贴到 html 编辑器,您可以这样做:
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);
}
}
这是一种变通方法,而且很丑陋,因为除非用户愿意,否则您永远不应该修改剪贴板内容,但它确实有效。另一方面,在粘贴操作后可以将剪贴板内容恢复到其原始状态。
编辑:
这是访问上下文菜单的方法,例如。 G。禁用它:
WebView webView = (WebView) htmlEditor.lookup(".web-view");
webView.setContextMenuEnabled(false);
不要尝试这一行,因为它总是 return 一个 NULL:
Button button = (Button) htmlEditor.lookup(".html-editor-paste");
从 HTMLEditor 获取粘贴按钮的唯一方法是@taha 的解决方案:
htmlEditor.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> { if (e.getTarget().toString().contains("html-editor-paste")) { System.out.println("paste pressed"); });
你好我想知道如何在JavaFX HTMLEditor.
中拦截粘贴事件你不能。 HTMLEditor 在内部使用 WebPage。基本上在粘贴事件期间,它通过
发送 "paste" 命令private boolean executeCommand(String command, String value) {
return webPage.executeCommand(command, value);
}
然后是
twkExecuteCommand(getPage(), command, value);
但是,您可以拦截所有隐式调用粘贴事件的内容,例如按钮单击或 CTRL+V 组合键,具体取决于您希望使用该事件执行的操作。
示例:
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);
}
}
至于您的 other question 仅将纯文本粘贴到 html 编辑器,您可以这样做:
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);
}
}
这是一种变通方法,而且很丑陋,因为除非用户愿意,否则您永远不应该修改剪贴板内容,但它确实有效。另一方面,在粘贴操作后可以将剪贴板内容恢复到其原始状态。
编辑:
这是访问上下文菜单的方法,例如。 G。禁用它:
WebView webView = (WebView) htmlEditor.lookup(".web-view");
webView.setContextMenuEnabled(false);
不要尝试这一行,因为它总是 return 一个 NULL:
Button button = (Button) htmlEditor.lookup(".html-editor-paste");
从 HTMLEditor 获取粘贴按钮的唯一方法是@taha 的解决方案:
htmlEditor.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> { if (e.getTarget().toString().contains("html-editor-paste")) { System.out.println("paste pressed"); });