Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to Implement "Caret Browsing" Using JavaFX 2

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.

like image 415
ProdigalBulldog Avatar asked Feb 28 '26 02:02

ProdigalBulldog


1 Answers

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.

like image 95
jewelsea Avatar answered Mar 02 '26 16:03

jewelsea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!