VIDURA VISHWA
VIDURA VISHWA

Reputation: 9

Windows on screen keyboard positioning error when using a dual-screen setup C#

Here’s the problem: When I first interact with the DateTime Picker, the keyboard appears on the secondary screen, which is expected. However, if I then interact with a Textbox, the keyboard appears on the main screen instead of staying on the secondary screen. Similarly, if I first interact with the Textbox, the keyboard appears on the secondary screen, but when I then interact with the DateTime Picker, it switches to the main screen. Additionally, when the keyboard does appear on the secondary screen for the second time, it only stays there for a few seconds before moving back to the main screen. This behavior is inconsistent and unexpected. Ideally, the keyboard should remain on the secondary screen when interacting with input fields on that screen.

IntPtr keyboardHandle = FindWindow("IPTip_Main_Window", null);
    Screen[] screens = Screen.AllScreens;
    if (keyboardHandle == IntPtr.Zero)
    {
        var startInfo = new ProcessStartInfo
        {    
              FileName = "C:\\Program Files\\Common Files\\microsoft shared\\ink\\TabTip.exe",
               UseShellExecute = true
        };
        Process oskProcess = Process.Start(startInfo);
        if (screens.Length < 2)
        {
            return;
        }
        System.Threading.Thread.Sleep(1000);
        keyboardHandle = FindWindow("IPTip_Main_Window", null);
    }
    Screen secondaryScreen = null;
   
    for (int i = 0; i < screens.Length; i++)
    {
        if (!screens[i].Primary)
        {
            secondaryScreen = screens[i];
            break;
        }
    }
    var workingArea = secondaryScreen.WorkingArea;
    SetWindowPos(keyboardHandle, IntPtr.Zero,
                 (int)(workingArea.Left * 1.05),
                 (int)(workingArea.Height * 0.5),
                 (int)(workingArea.Width), // 100% of screen width
                 (int)(workingArea.Height * 0.8), // 80% of screen height
                 SWP_NOZORDER | SWP_NOACTIVATE);
}

Tried to change SetWindowPos with different flags its not working

Upvotes: 0

Views: 38

Answers (0)

Related Questions