Tips and tricks for C# Metro developers : Handling the keyboard events - Eternal Coding - HTML5 / JavaScript / 3D development - Site Home - MSDN Blogs

Tips and tricks for C# Metro developers : Handling the keyboard events


 

Tips and tricks for C# Metro developers : Handling the keyboard events

  • Comments 2

I have some users that ask me how can I handle the keyboard ? Should I add an event Handler on a top root grid?

The response is no Sourire. The better solution is to use the KeyUp/KeyDown event of the CoreWindow which is able to catch all the keyboard strokes:

protected override void OnNavigatedTo(NavigationEventArgs e)
{ 
    Window.Current.CoreWindow.KeyUp += CoreWindow_KeyUp;
}

void CoreWindow_KeyUp(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
    switch (args.VirtualKey)
    {
        case Windows.System.VirtualKey.Number1:
            break;
        case Windows.System.VirtualKey.Number2:
            break;

} } protected override void OnNavigatedFrom(NavigationEventArgs e) { Window.Current.CoreWindow.KeyUp -= CoreWindow_KeyUp; }

Please note that the KeyEventArgs class provides a Handled property in order to stop the propagation of the event.

Leave a Comment
  • Please add 8 and 4 and type the answer here:
  • Post
  • my god after 4 hours of research this small piece of code worked for me :)

  • Thank you! I have just spent a couple of hours on this until I found your post :)

Page 1 of 1 (2 items)