Page-global keyboard events in Windows Store Apps Page-global keyboard events in Windows Store Apps wpf wpf

Page-global keyboard events in Windows Store Apps


Try using CoreWindow.KeyDown. Assign the handler in your page and I believe it should intercept all keydown events.

public MyPage(){    CoreWindow.GetForCurrentThread().KeyDown += MyPage_KeyDown;}void MyPage_KeyDown(CoreWindow sender, KeyEventArgs args){    Debug.WriteLine(args.VirtualKey.ToString());}


CoreWindow.GetForCurrentThread().KeyDown will not capture events if it the focus is on some other grid/webView/text box, so instead use AcceleratorKeyActivated event. Irrespective of where the focus is it will always capture the event.

public MyPage(){    Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += AcceleratorKeyActivated;}private void AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args){    if (args.EventType.ToString().Contains("Down"))    {        var ctrl = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control);        if (ctrl.HasFlag(CoreVirtualKeyStates.Down))        {            switch (args.VirtualKey)            {                case VirtualKey.A:                    Debug.WriteLine(args.VirtualKey);                    Play_click(sender, new RoutedEventArgs());                    break;            }        }    }}