ConsS
ConsS

Reputation: 25

Controlling the Windows on screen keyboard in C#

I have a Windows on screen keyboard implemented in my applications as such:

    [ComImport, Guid("4ce576fa-83dc-4F88-951c-9d0782b4e376")]
    class UIHostNoLaunch
    {
    }

    [ComImport, Guid("37c994e7-432b-4834-a2f7-dce1f13b834b")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface ITipInvocation
    {
        void Toggle(IntPtr hwnd);
    }

    [DllImport("user32.dll", SetLastError = false)]
    static extern IntPtr GetDesktopWindow();

    public static void ShowWindowsKeyboard()
    {
        var uiHostNoLaunch = new UIHostNoLaunch();
        var tipInvocation = (ITipInvocation)uiHostNoLaunch;            
        tipInvocation.Toggle(GetDesktopWindow());           
        Marshal.ReleaseComObject(uiHostNoLaunch);
    }

The keyboard shows up using a toggle function, my question is - can you make the keyboard appear with a specific function and disappear with another function instead of using toggle?

For example textbox_OnGotFocus(EventArgs e) { some code that makes the keyboard appear } textBox_OnLostFocus(EventArgs e) {some code that makes the keyboard dissapear }

Upvotes: 1

Views: 467

Answers (1)

Eliahu Aaron
Eliahu Aaron

Reputation: 4552

How about this:

    static readonly string OnScreenKeyboardProgramName = "osk";

    public static void StartOnScreenKeyboard()
    {
        System.Diagnostics.Process.Start(OnScreenKeyboardProgramName);
    }

    public static void StopOnScreenKeyboard()
    {
        System.Diagnostics.Process[] processes = 
            System.Diagnostics.Process.GetProcessesByName(OnScreenKeyboardProgramName);

        if (processes.Length > 0)
        {
            processes[0].Kill();
            processes[0].WaitForExit();
            processes[0].Dispose();
        }
    }

Upvotes: 1

Related Questions