Gillardo
Gillardo

Reputation: 9828

JavaFX virtual keyboard

Is there such a thing? I have googled javafx virtual keyboard, but nothing seems to appear that is valid. I saw this article, but no code or example

http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/embed.htm

Does anyone have prior experience with a virtual keyboard in a javafx application? I need to be able to show either a normal keyboard or numeric keypad when a textfield is focused.

Upvotes: 6

Views: 17338

Answers (4)

selami tastan
selami tastan

Reputation: 102

Maven dependeny :

    <!-- onscreen-keyboard -->
    <dependency>
        <groupId>org.comtel2000</groupId>
        <artifactId>fx-onscreen-keyboard</artifactId>
        <version>8.2.5</version>
    </dependency>
    <!-- onscreen-keyboard -->

Static function showVirtualKeyboard :

public static void showVirtualKeyboard(Stage primaryStage){
    Platform.runLater(() -> {
        if(keyBoardPopup == null){
            KeyboardPane kb = new KeyboardPane();
            kb.setLayer(DefaultLayer.NUMBLOCK);
            try {
                kb.load();

            } catch (Exception e) {
            }

            keyBoardPopup = new KeyBoardPopup(kb);

            Scene scene = new Scene(new Group(), 0.1, 0.1);
            Stage stage = new Stage();
            stage.initOwner(primaryStage);
            //stage.initStyle(StageStyle.UTILITY);
            stage.initStyle(StageStyle.UNDECORATED);
            stage.setScene(scene);
            //stage.initModality(Modality.APPLICATION_MODAL);
            stage.setAlwaysOnTop(true);
            keyBoardPopup.show(stage);

            keyBoardPopup.registerScene(primaryStage.getScene());
            keyBoardPopup.setVisible(true);
            kb.setOnKeyboardCloseButton(event -> keyBoardPopup.setVisible(false));


            String key = "virtualkeyboard";
            double scale = Options.getScale(key);
            double x = Options.getWindowX(key);
            double y = Options.getWindowY(key);
            boolean inBounds = true;
            if(x != 0 && y != 0 && y > 0){
                ObservableList<Screen> screenSizes = Screen.getScreens();
                if(screenSizes.size() == 1){
                    if(x >= screenSizes.get(0).getBounds().getMaxX() || y >= screenSizes.get(0).getBounds().getMaxY()){
                        inBounds = false;
                    }
                }
                if(inBounds){
                    keyBoardPopup.setX(x);
                    keyBoardPopup.setY(y);
                }
            }
            ChangeListener<Number> changeListenerX = null, changeListenerY = null;
            changeListenerX = (observable, oldValue, newValue) -> Options.setWindowX(key, newValue.doubleValue());
            changeListenerY = (observable, oldValue, newValue) -> Options.setWindowY(key, newValue.doubleValue());
            keyBoardPopup.xProperty().addListener(changeListenerX);
            keyBoardPopup.yProperty().addListener(changeListenerY);

            kb.setScale(scale < 0 ? 2 : scale);
            //Options.setScale(key, -1);
            kb.scaleProperty().addListener((observable, oldValue, newValue) -> Options.setScale(key, newValue.doubleValue()));
        }else{
            keyBoardPopup.setVisible(true);
        }
    });
}

Some Options functions

private static Preferences preferences = Preferences.userRoot();
public static Preferences getPreferences() { try { preferences.sync(); } catch (Exception e) { } return preferences; }


public synchronized static double getWindowX(String window) {
    return getPreferences()
            .getDouble(ApplicationName + ".system.window." + window + ".x", 0);
}

public synchronized static void setWindowX(String window, double x) {
    getPreferences().putDouble(ApplicationName + ".system.window." + window + ".x", x);
}

public synchronized static double getWindowY(String window) {
    return getPreferences()
            .getDouble(ApplicationName + ".system.window." + window + ".y", 0);
}

public synchronized static void setWindowY(String window, double y) {
    getPreferences().putDouble(ApplicationName + ".system.window." + window + ".y", y);
}

public synchronized static double getScale(String window) {
    return getPreferences()
            .getDouble(ApplicationName + ".system.scale." + window + ".x", -1);
}

public synchronized static void setScale(String window, double x) {
    getPreferences().putDouble(ApplicationName + ".system.scale." + window + ".x", x);
}

Upvotes: 2

Surya Hardiansyah
Surya Hardiansyah

Reputation: 141

if you are confused, here, catch:

1.right click your project (in Netbeans)

2.click Properties-->Run-->

3.Fill the [VM Options] with:

  -Dcom.sun.javafx.isEmbedded=true 
  -Dcom.sun.javafx.touch=true 
  -Dcom.sun.javafx.virtualKeyboard=javafx

4.OK,done

Upvotes: 2

NTwoO
NTwoO

Reputation: 75

To be sure, you must add the flags

  • -Dcom.sun.javafx.isEmbedded=true
  • -Dcom.sun.javafx.touch=true
  • -Dcom.sun.javafx.virtualKeyboard=javafx

to the javavm and not to the application... (added as post, since I can't comment...)

Is customising the keyboard layout required? check /com/sun/javafx/scene/control/skin/caspian/fxvk.css in ${JRE/JDK_INSTALL}/jre/lib/ext/jfxrt.jar for the css fields influencing the keyboard.

Upvotes: 0

jewelsea
jewelsea

Reputation: 159536

Yes, there is such a thing as JavaFX virtual keyboard in the Oracle Java 8 distribution, though it is not documented or supported by Oracle outside of the embedded version of JavaFX.

However, the virtual keyboard does seem to ship with the Oracle desktop Java 8 JRE and it does seem to work there if you toggle some undocumented and unsupported system properties.

Try:

  • -Dcom.sun.javafx.isEmbedded=true

And maybe also

  • -Dcom.sun.javafx.touch=true
  • -Dcom.sun.javafx.virtualKeyboard=javafx

Some related sketchy info on the internet:

The guys over at javafxports might know more, so you could try asking there if you want more information (if you are targeting one of their devices).

Upvotes: 11

Related Questions