Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard shortcuts in Metro apps

How do I capture a keyboard shortcut, no matter which control has focus? I don't want to go and write the same thing for each possible control that the user could give focus to. So how can I watch for a page-wide/control-independent shortcut?

like image 710
Tommy Avatar asked Feb 07 '13 12:02

Tommy


2 Answers

Add this code to the constructor it will handle a global key down and key

Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
        Window.Current.CoreWindow.KeyUp += CoreWindow_KeyUp;

heres their events

void CoreWindow_KeyUp(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
    {
        //this.Frame.Navigate(typeof(MainPage));
        var key = args.VirtualKey;
        string aa = args.ToString();
    }

    void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
    {
        //this.Frame.Navigate(typeof(MainPage));
        var key = args.VirtualKey;
        string aa = args.ToString();
    }

you can ahndle your own logic inside this event.

like image 116
Anobik Avatar answered Sep 21 '22 01:09

Anobik


How about setting an event handler at the root element? I think event will eventually reach the parent control, if it is not handled anywhere else. Here's what I would do for a simple KeyDown event.

<common:LayoutAwarePage
x:Name="pageRoot"
//elided
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" KeyDown="MyEventHandler">
like image 36
Dido Avatar answered Sep 19 '22 01:09

Dido