尝试实现“插入符浏览”使用 JavaFX 2

发布于 2025-01-07 13:14:49 字数 696 浏览 6 评论 0原文

我正在尝试使用 JavaFX 2.0 编写一个应用程序,其中包含一个 Web 浏览器控件,该控件允许用户仅使用键盘浏览 HTML 页面上的文本和图像 - 基本上就像“插入符号浏览” ” 在 Internet Explorer 中。

目标是能够选择文本或图像位并将它们复制到变量中以进行进一步操作,而无需使用鼠标。

我在这里查看了 HTMLEditor 控件: http://docs.oracle.com/javafx/2.0/ui_controls/editor .htm#CHDBEGDD 但我不需要任何使用户界面混乱的编辑功能,文档中说:

格式化工具栏是在实现时提供的 成分。您无法切换它们的可见性。

WebView 似乎是一个合乎逻辑的选择(http://docs.oracle.com/ javafx/2.0/webview/jfxpub-webview.htm),但我不确定如何将光标移到页面上。

任何建议将不胜感激。

I'm trying to write an application using JavaFX 2.0 that includes a web browser control that allows a user to navigate through the text and images on a HTML page using only the keyboard -- basically like "caret browsing" in Internet Explorer.

The goal is to be able to select bits of text or images and copy them to a variable for further manipulation without using a mouse.

I took a look at the HTMLEditor control here:
http://docs.oracle.com/javafx/2.0/ui_controls/editor.htm#CHDBEGDD
but I don't need any editing capability cluttering up the UI, and the documentation says:

The formatting toolbars are provided in the implementation of the
component. You cannot toggle their visibility.

WebView seems like a logical choice (http://docs.oracle.com/javafx/2.0/webview/jfxpub-webview.htm), but I'm not sure how to get a cursor onto the page.

Any advice would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御弟哥哥 2025-01-14 13:14:49

目前的 WebView 对插入符号浏览的支持在我看来充其量是不完整的。

以下是我在运行快速测试时发现的结果:

  1. 我可以调用 webView.requestFocus 来让 WebView 请求焦点响应按键,但它只是对 WebView 作为一个整体进行操作,而不是对其中的各个组件进行操作Web 视图。
  2. WebView 没有实现类似于 TextInputControl 用于对选择进行细粒度的程序化管理。
  3. WebView 确实允许您选择文本。但是,我必须通过鼠标拖动来启动选择,然后我可以使用键盘来放大或缩短选择(向左或向右箭头键用于按字符选择字符,按 ctrl + 向左或向右箭头键用于按单词选择)单词选择 - 向上和向下箭头不影响选择)。
  4. 在 WebView 中选择一些文本后,我可以按 Ctrl-C(在 Windows 中)将其复制到剪贴板并将文本粘贴到另一个程序中。仅复制原始文本 - 未复制关联的样式/html 信息和图像。
  5. 要复制图像,我必须右键单击图像并从下拉菜单中选择“复制图像”,然后我可以将图像粘贴到 MS Paint 中 - 我无法找到无需鼠标即可执行此操作的方法。
  6. 在其他浏览器中,我可以按 TAB 和 Shift + TAB 从一个超链接转到下一个超链接 - 在 WebView 中,一旦获得焦点,TAB 就会从一个控件(例如 WebView 中的 html 文本字段)转到下一个控件(例如文本字段中的 html 按钮)。
  7. 退格键的作用与其他浏览器中一样(带您到上一页)。

上述限制以及我未测试的其他限制可能会使您难以完成您想要做的事情。您可以尝试使用 eventfilter,然后生成鼠标事件来启动选择和复制过程,但这对我来说听起来很困难,即便如此,目前还没有JavaFX 中用于生成鼠标事件的公共 API,只有一个不稳定的 com.sun api。

WebView 确实公开了 文档对象模型,文档为可通过 JavaScript 编写脚本。尝试使用事件过滤器捕获关键事件,侦听文档属性的更改并在适当的时间对 WebView 执行 JavaScript 以获取和设置当前选择。实施起来似乎也有点棘手。

使用当前的 WebView 控件和公共 API 尽可能多地完成任务,并在 http://javafx-jira.kenai 上记录问题.com 当您遇到缺点时。

The current WebView support for caret browsing seems patchy at best to me.

Here is what I found running a quick test:

  1. I can invoke webView.requestFocus to have the WebView request focus for responding to key presses, but it just operates on the WebView as a whole, not individual components within the WebView.
  2. WebView does not implement a selection management API similar to TextInputControl for fine grained programmatic management of selections.
  3. WebView does allow you to select text. However, I had to initiate the selection with a mouse drag, and after that I could use a keyboard to enlarge or shorten the selection (left or right arrow keys for a character by character selection and ctrl + left or right arrow keys for word by word selection - up and down arrows did not affect the selection).
  4. After selecting some text in WebView I could press Ctrl-C on it (in Windows) to copy it to a clipboard and paste the text into another program. Only raw text was copied - associated style/html info and images were not copied.
  5. To copy images I had to right click on an image and select Copy Image from a drop down menu and I could paste the image into MS Paint - I could not find a way to do this without a mouse.
  6. In other browsers I can press TAB and Shift + TAB to go from one hyperlink to the next - in WebView, once it has focus, TAB will just go from one control (e.g. an html text field in the WebView) to the next (e.g. an html button in the text field).
  7. The backspace key works as in other browsers (takes you to the previous page).

The above restrictions, and likely others I didn't test for, will likely make it hard to accomplish what you are trying to do. You could try stuff such as capturing keypress events using an eventfilter, then generating mouse events to initiate the selection and copy process, but that sounds difficult to me, and even then, there is currently no public API in JavaFX to generate mouse events, only an unstable com.sun api.

WebView does expose a document object model, and the document is scriptable by JavaScript. Try capturing key events with an eventfilter, listening to the document property for changes and executing JavaScript against the WebView at appropriate times to get and set the current selection. This also seems a little tricky to implement well.

Accomplish as much as you can with the current WebView control and public API and log issues at http://javafx-jira.kenai.com as and when you encounter short-comings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文