更改JavaFX虚拟键盘的显示

时间:2017-06-15 08:49:10

标签: javafx touch virtual-keyboard

我的开发系统包含一个连接了三个显示器的Windows PC。第三个显示是我的触摸屏显示。我指示Windows使用此屏幕作为我的触摸屏显示器,使用" Tablet PC设置"来自控制面板。

我的应用程序是一个包含TextField的简单JavaFX触摸屏应用程序。要显示虚拟键盘,我将以下设置设置为true:

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

我的问题是键盘出现了,但显示错误。它显示在主监视器上,而不是设置为触摸监视器的第三个监视器。

有没有办法在当前系统配置中在触摸显示器上显示虚拟键盘?例如,告诉键盘它的所有者应用程序在哪里,它显示在正确的监视器上?

1 个答案:

答案 0 :(得分:1)

了解如何将显示键盘的显示器更改为显示应用程序的显示器。

将更改侦听器附加到textField的focus属性。执行更改侦听器时,检索键盘弹出窗口。然后找到显示应用程序的监视器的活动屏幕边界,并将键盘x坐标移动到此位置。

通过将autoFix设置为true,键盘将确保它不在(部分)显示器外,设置autoFix将自动调整y坐标。如果未设置autoFix,则还必须手动设置y坐标。

@FXML
private void initialize() {
  textField.focusedProperty().addListener(getKeyboardChangeListener());
}

private ChangeListener getKeyboardChangeListener() {
    return new ChangeListener() {
        @Override
        public void changed(ObservableValue observable, Object oldValue, Object newValue) {
            PopupWindow keyboard = getKeyboardPopup();

            // Make sure the keyboard is shown at the screen where the application is already shown.
            Rectangle2D screenBounds = getActiveScreenBounds();
            keyboard.setX(screenBounds.getMinX());
            keyboard.setAutoFix(true);
        }
    };
}

private PopupWindow getKeyboardPopup() {
    @SuppressWarnings("deprecation")
    final Iterator<Window> windows = Window.impl_getWindows();

    while (windows.hasNext()) {
        final Window window = windows.next();
        if (window instanceof PopupWindow) {
            if (window.getScene() != null && window.getScene().getRoot() != null) {
                Parent root = window.getScene().getRoot();
                if (root.getChildrenUnmodifiable().size() > 0) {
                    Node popup = root.getChildrenUnmodifiable().get(0);
                    if (popup.lookup(".fxvk") != null) {
                        return (PopupWindow)window;
                    }
                }
            }
            return null;
        }
    }
    return null;
}

private Rectangle2D getActiveScreenBounds() {
    Scene scene = usernameField.getScene();
    List<Screen> interScreens = Screen.getScreensForRectangle(scene.getWindow().getX(), scene.getWindow().getY(),
            scene.getWindow().getWidth(), scene.getWindow().getHeight());
    Screen activeScreen = interScreens.get(0);
    return activeScreen.getBounds();
}