Detecting event textkeya HTML textkeya in JavaFX WebView

I want to print the contents of an HTML text box inside a WebView as soon as I print it.
PS: I tried to listen keyEventfrom webViewfor some reason, it did not work.

+5
source share
1 answer
  • If you are trying to print the contents of a JavaFX TextArea into a WebView, you must add listeners to TextArea, not to WebView.

  • If you are trying to listen to an event in the HTML TextArea tag inside an HTML page in a WebView, you must add listeners to the document model:

    // we need this to wait till document load
    webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
        public void changed(ObservableValue ov, State oldState, State newState) {
            if (newState == Worker.State.SUCCEEDED) {
    
                // note next classes are from org.w3c.dom domain
                EventListener listener = new EventListener() {
                    public void handleEvent(Event ev) {
                        System.out.println(ev.getType());
                    }
                };
    
                Document doc = webEngine.getDocument();
                Element el = doc.getElementById("textarea");
                ((EventTarget) el).addEventListener("keypress", listener, false);
            }
        }
    });
    webEngine.loadContent("<textarea id='textarea'></textarea>");
    
+10
source

All Articles