1

I have partially solved the following problem: JavaFX WebView / WebEngine show on-screen-keyboard automatically for each text input

I stucked at the 6th point because I would like to use the built in JavaFX virtual keyboard but I can not find any reference how can trigger the displaying of it.

Do you know any solution for this? If it is possible I do not want to use 3rd party library.

Community
  • 1
  • 1

1 Answers1

1

I am going to answer my question because I found a solution.

First of all I added an event listener for all input tags on webpage, after page loaded:

private void addEventListenersToDOM() {
    webview.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
        @Override
        public void changed(ObservableValue<? extends State> ov, State oldState, State newState) {
            if (newState == State.SUCCEEDED) {
                JSObject win = (JSObject) webview.getEngine().executeScript("window");
                win.setMember("javaFXVirtualKeyboard", new JavaFXVirtualKeyboard());

                String script = 
                          "var inputsList = document.getElementsByTagName('input');" 
                        + "for (var index = 0; index < inputsList.length; ++index) { "
                        +      "inputsList[index].addEventListener('focus', function() { javaFXVirtualKeyboard.show() }, false); " 
                        +      "inputsList[index].addEventListener('focusout', function() { javaFXVirtualKeyboard.hide() }, false); "
                        + "}";
                webview.getEngine().executeScript(script);
            }
        }
    });
}

And the key point, how I triggering the keyboard displaying and hiding:

public class JavaFXVirtualKeyboard {

    public void show() {
        FXVK.init(webview);
        FXVK.attach(webview);
    }

    public void hide() {
        FXVK.detach();
    }
}

One note: FXVK class is not an API so we get a warning message in all cases but it works without any bug.

Discouraged access: The type 'FXVK' is not API (restriction on required library 'C:\Program Files\Java\jre1.8.0_91\lib\ext\jfxrt.jar')