How to use Windows On-Screen Keyboard in C# WinForms
I am now launching the "Touch Keyboard" as opposed to the "On-Screen Keyboard" (which is the keyboard I wanted on Windows 8 anyway) with:
string progFiles = @"C:\Program Files\Common Files\Microsoft Shared\ink";
string keyboardPath = Path.Combine(progFiles, "TabTip.exe");
this.keyboardProc = Process.Start(keyboardPath);
This works on my Win7 and Win8, regardless of my 32-bit app on 64-bit OS. However, I still have the problem of programmatically closing the keyboard when I'm done. The process, this.keyboardProc
, does not seem to get the handle, and immediately has property HasExited = true
. This means my attempts to close or kill it fail.
According to this thread, if the user manually opens the keyboard (or I programmatically launch it), the keyboard will not automatically close/hide when the text field loses focus: Windows 8 - How to Dismiss Touch Keyboard? I tried the workaround of setting the focus to a hidden button, but since I launched the keyboard myself, it doesn't close automatically.
I had trouble closing the on screen keyboard. You can start the Touch Keyboard with
string progFiles = @"C:\Program Files\Common Files\Microsoft Shared\ink";
string onScreenKeyboardPath = System.IO.Path.Combine(progFiles, "TabTip.exe");
onScreenKeyboardProc = System.Diagnostics.Process.Start(onScreenKeyboardPath);
and close all keyboards with
//Kill all on screen keyboards
Process[] oskProcessArray = Process.GetProcessesByName("TabTip");
foreach (Process onscreenProcess in oskProcessArray)
{
onscreenProcess.Kill();
}
For some reason onScreenKeyboardProc.Kill() or .Close() doesn't work.
For the keyboard to open up automatically, the controls have to implement some UI Automation control patterns, specifically ITextProvider/IValueProvider. It is a nuisance, but it works (and it is a cleaner way than launching some *.exe)
I wrote a sample app and a short article here: http://blog.tombam.net/implementing-textbox-with-on-screen-touch-keyboard-part-1/