Going the InputBindings way: Part 3 - What’s the WinRT equivalent of KeyBindings?

Link to part 2 of this series: Going the InputBindings way- Part 2 - .NET 3.5

Now, if you have already worked on some windows 8 apps and cared about keyboard accessiblity, or maybe just from the title of of this post, there is no KeyBinding to perform an action.

The way you would want to implement keyboard accessibility in Windows 8 apps is by using access keys and accelerator keys. In a nutshell, an access key is a shortcut to a piece of UI in your app. And an accelerator key is a shortcut to a command. This can be defined on the controls in your app by specifying the following properties: AutomationProperties.AccessKey and AutomationProperties.AcceleratorKey.

Bear in mind that setting these properties does not actually enable keyboard functionality. It only helps the user to understand what keys to use to perform certain actions using technoligies such as screen readers and other assistive techonologies. To enable the keyboard functionality you would still need to handle the keyboard events such as KeyDown and KeyUp.

To get a deeper understanding of this, read this article http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868161.aspx.

Now lets look at some code.

I created a project from the Blank App (XAML) project template in Visual Studio Express 2012 for Windows 8. And replaced the Grid that’s placed by default in MainPage.xaml with the following

    Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
      Content="Test Button"
      AutomationProperties.AcceleratorKey="Control A" />
      Name="TestTextBlock" />

And in the code-behind I implemented the click event handler as follows:

    private void TestButton_Click(object sender, RoutedEventArgs e)
        this.TestTextBlock.Text = "Yippee!";

Clicking on the button results in the text getting updated, however, using the accelerator key does not. However, the interesting thing to note is that if you open Narrator when your app is running and focus on the button, the narrator will tell you that the shortcut is Control A. Which is kind of cool.

Now, getting control A to do the click is simply a matter of implementing the KeyDown/Up handlers based on the behaviour that you want and in this case populate the TextBox with Yippee!.

Further reading:

Till my next post.