Reputation: 877
My development system contains of a Windows pc with three displays attached to it. The third display is my touch screen display. I've instructed Windows to use this screen as my touch screen display with the "Tablet PC Settings" from Control Panel.
My application is a simple JavaFX touch screen application containing a TextField. To show the virtual keyboard I've set the following settings to true:
My issue is that the keyboard is showing up, but on the wrong monitor. It shows on the primary monitor, instead of the third monitor that is set to be the touch monitor.
Is there a way to show the virtual keyboard on my touch monitor in the current system configuration? For example by telling the keyboard where it's owner application is so it displays on the correct monitor?
Upvotes: 0
Views: 1466
Reputation: 877
Found out how to change the monitor on which the keyboard is shown, to the monitor where the application is shown.
Attach a change listener to the focused property of your textField. When executing the change listener, retrieve the keyboard popup. Then find the active screen bounds of the monitor where the application is shown and move the keyboard x-coordinate to this location.
By setting autoFix to true, the keyboard will make sure its not (partially) outside your monitor, setting autoFix will adjust the y-coordinate automatically. If you don't set autoFix, you also have to set the y-coordinate manually.
@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();
}
Upvotes: 1