Developing for Modern Windows

Tips, tricks, and guides for developing on modern Windows platforms

Detecting when the On-screen Keyboard is Active

When your Windows 8/8.1 app is running on a tablet and accepting keyboard input there is a fair chance that the software on-screen keyboard is being used. This is all well and good, except that the on-screen keyboard can take up a huge amount of screen real estate, especially in landscape mode (around 50% of the screen!).

So to make sure your app still works well with half of the screen taken up by a keyboard you should detect the on-screen keyboard just as you would check for other ways your app’s size on screen could change (e.g. snapping, switching between portrait and landscape, etc.).

It’s very easy to do. You just add handlers to detect when the keyboard goes up and down:

AddHandler InputPane.GetForCurrentView.Showing, AddressOf KeyboardUp
AddHandler InputPane.GetForCurrentView.Hiding, AddressOf KeyboardDown

Now when the keyboard is raised the KeyboardUp method will be called, and when the keyboard goes down your KeyboardDown method will be called. In these methods you can make any necessary changes to the UI to suit the different screen layout, e.g.:

Private Sub KeyboardUp()
' make the main grid row shorter
LayoutRoot.MainGridRow.Height = New GridLength(100)
End Sub

Private Sub KeyboardDown()
' return the main grid row to full height
LayoutRoot.MainGridRow.Height = New Gridlength(300)
End Sub

And if you make full use of view states you can of course trigger a completely different view for when the keyboard is up!

Tags: , , , , , ,

Leave a Reply

Your email address will not be published.