(Print this page)

Controlling the Windows 10 On-Screen keyboard from a .Net Windows Forms application
Published date: Friday, May 13, 2016
On: Moer and Éric Moreau's web site

On-screen keyboard is very known when using smartphones and tablets. A little less used with WPF applications and even less with legacy Windows forms application. But that doesn’t mean it can’t be done. With more and more users having touch laptops and monitors using your Windows Forms applications, it can make sense sometimes to show (or hide) the on-screen keyboard panel.

You don’t need to reinvent the wheel and to build one by yourself. Windows is providing one that integrates into your application nicely.

Downloadable demo application

This month demo application is provided in both VB and C#. It was created using Visual Studio 2015 but the same code could be used in older versions as well.

Figure 1: the demo application in action



TabTip.exe

The on-screen keyboard is not a component or a control to which you can add a reference to your project and use it as a regular object in your application. It is a process that you can start and that will live on its own still interacting with your application (if a control accepting keyboard input is currently having the focus).

The TabTip application is installed by default into the C:\Program Files\Common Files\microsoft shared\ink folder. If you don’t have a standard installation, you will need to modify the path in the application.

If I am not mistaken, TabTip has been around since Windows XP.

Building the UI

The UI for this demo application is really simple.

Two buttons and a textbox. That’s it. Don’t look for anything fancy, you won’t find any!

The first button will be used to show the on-screen keyboard. The second one to hide it. The textbox is used to receive the input.

Generally speaking, one thing that you need to consider if you know that your users are using their fingers to navigate your application is that you need to ensure that the controls are suitable for this (for example, they are large enough and correctly spaced so that the fat fingers are doing exactly what they want the first time they touch the screen).

Showing the on-screen keyboard

When you want to show the on-screen keyboard panel, you will need to start the TabTip.exe process. Process.Start is the way to go to start it. As shown in the following snippet:

Private Sub btnShowKeyboard_Click(sender As Object, e As EventArgs) Handles btnShowKeyboard.Click
    If mprcTabTip Is Nothing OrElse mprcTabTip.HasExited Then
        If mprcTabTip IsNot Nothing AndAlso mprcTabTip.HasExited Then
            mprcTabTip.Close()
        End If
        mprcTabTip = Process.Start("C:\Program Files\Common Files\microsoft shared\ink\TabTip.exe")
        'give the focus to the textbox
        TextBox1.Focus()
    End If
End Sub

After the process has been started, you should give the focus to control you want so the user can just start using the on-screen keyboard.

As you can see on figure 1, not only the keyboard is shown on the screen but also the auto-complete just like when the panel is shown from a real modern application.

Also when you are using the panel and the textbox has the focus, its content is automatically updated.

Isn’t it a nice feature?

Hiding the on-screen keyboard

You may want to hide the keyboard from your application even if your user can close the keyboard by clicking on the X from the top-left of the keyboard panel.

From what I have read, sometimes the TabTip process starts 2 tasks. This is why the following code loops through all the processes and doesn’t exit after the first one has been found:

Private Sub btnHideKeyboard_Click(sender As Object, e As EventArgs) Handles btnHideKeyboard.Click
    For Each pkiller As Process In Process.GetProcesses
        If String.Compare(pkiller.ProcessName, "tabtip", True) = 0 Then
            pkiller.Kill()
        End If
    Next
End Sub

As this code is doing, you just need to kill the process and the keyboard will disappear.

This method should be called when you close the form just to be sure that the keyboard is not left behind on the screen.

Conclusion

Offering the on-screen keyboard to your users is far then enough to claim that your application is touch friendly but it is a beginning. As already said, buttons need to be larger to ease the fat-fingers like mine to click THE button they want on the first hit. You also need to space them correctly.

As shown here, even legacy Windows forms application can easily offer the on-screen keyboard feature if your version of Windows already supports it.


(Print this page)