0

I have written a code to position OSK keyboard to the bottom left of the screen when the process has stared. What I have found out is that the code works correctly when I have placed a debugger at the point when the handle is active. But as soon as I remove all the debugging points, and run the windows form, the OSK is not position to bottom left. What I have found is the handle KeyboardWnd has value 0 in it when all the debugging points are removed.

I have tried to delay the process as well to check if the handle loads till then but it does not work. Also "await" is not an option since it needs async for its call

Below is the code for reference

 string osk = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "system32"), "osk.exe");

            //  start the keyboard process
            ProcessStartInfo startInfo = new ProcessStartInfo(osk);                
            Process tmp = Process.Start(startInfo);
            IntPtr KeyboardWnd = GetTabTipWindowHandle();

              System.Threading.Thread.Sleep(20);

            MoveKeyBoard(KeyboardWnd, 0, Screen.PrimaryScreen.Bounds.Height / 4*3);

The MoveKeyBoard inturn calls for setwindowpos method as below

 public static bool MoveKeyBoard(IntPtr hWnd, int ToX, int ToY)
    {           
        return SetWindowPos(hWnd, (IntPtr)0, ToX, ToY, Screen.PrimaryScreen.Bounds.Width/2, Screen.PrimaryScreen.Bounds.Height/4, SWP.NOZORDER | SWP.SHOWWINDOW | SWP.NOCOPYBITS);
    }

 public static IntPtr GetTabTipWindowHandle()
    {
        if (TabTip.UseTabTipKeyboard)
            return FindWindow("IPTip_Main_Window", null);
        else            
            return FindWindow("OSKMainClass", null);

    }
  • Where is method GetTabTipWindowHandle()? – jdweng Sep 15 '17 at 09:26
  • I have made the edit. Please have a look. – Prachi Mayekar Sep 15 '17 at 09:35
  • The advantage of a debugger is that it slows down your program. So OSK has enough time to create the window and FindWindow() can find it. You must wait to give it a chance to initialize. Adding tmp.WaitForInputIdle().is a minimum requirement. – Hans Passant Sep 15 '17 at 10:29
  • Try FindwindowEx. See Pinvoke : http://www.pinvoke.net/default.aspx/user32.FindWindowEx. there is an explanation on following posting using c++ : https://stackoverflow.com/questions/16530871/findwindow-does-not-find-the-a-window – jdweng Sep 15 '17 at 10:31

1 Answers1

0

I got the solution for this. Below code works fine

 RegistryKey myKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Osk", true);

                myKey.SetValue("WindowLeft", 0, RegistryValueKind.DWord);
                myKey.SetValue("WindowTop", Screen.PrimaryScreen.Bounds.Height / 4 * 3, RegistryValueKind.DWord);
                myKey.SetValue("WindowWidth", Screen.PrimaryScreen.Bounds.Width / 2, RegistryValueKind.DWord);
                myKey.SetValue("WindowHeight", Screen.PrimaryScreen.Bounds.Height / 4, RegistryValueKind.DWord);
                 IntPtr KeyboardWnd = GetTabTipWindowHandle();


                   MoveKeyBoard(KeyboardWnd, 0, Screen.PrimaryScreen.Bounds.Height / 4 * 3);