更改JavaFX虚拟键盘的显示
问题描述:
我的开发系统包含一个带有三个显示器的Windows电脑。第三个显示器是我的触摸屏显示器。我已经通过控制面板指示Windows使用此屏幕作为我的触摸屏显示和“Tablet PC设置”。更改JavaFX虚拟键盘的显示
我的应用程序是一个简单的包含TextField的JavaFX触摸屏应用程序。要显示虚拟键盘,我进行以下设置为true:
- -Dcom.sun.javafx.isEmbedded =真
- -Dcom.sun.javafx.touch =真
- -Dcom。 sun.javafx.virtualKeyboard = javafx
我的问题是键盘显示出来,但在错误的显示器上。它显示在主显示器上,而不是第三台设置为触摸显示器的显示器。
有没有办法在我的触摸显示器上显示当前系统配置中的虚拟键盘?例如,通过告诉键盘它的所有者应用程序在哪里,它显示在正确的显示器上?
答
找到了如何将显示键盘的显示器更改到显示应用程序的显示器。
将更改侦听器附加到textField的焦点属性。执行更改侦听器时,检索键盘弹出窗口。然后找到显示应用程序的显示器的活动屏幕边界,并将键盘的x坐标移动到该位置。
通过设置autoFix为true,键盘将确保它不在显示器外部(部分),设置autoFix将自动调整y坐标。如果你没有设置自动补偿,你还必须手动设置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();
}