Keyboard Shortcuts in Metro Applications

How to get a shortcut on the keyboard, no matter which control has focus? I don’t want to go and write the same thing for every possible control that the user could focus on. So how can I look at a page-independent / no-control shortcut?

+4
source share
3 answers

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

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

takes 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 configure your own logic inside this event.

+4
source

How to install an event handler in the root element? I think the event will eventually reach parental control if it is not handled anywhere. Here is 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"> 
+1
source

The best solution was to use the root element, as suggested earlier, because global labels use functions of the same class that apply to each page, which means that everyone has to process each label separately on each page or risk errors.

My XAML was configured as follows:

 <Page Name="root" ...> ... </Page> 

My C # to manage this page looks like this:

 ... namespace DenisQuiz.UWP { public sealed partial class StudyADeck : Page { ... public StudyADeck() { ... // Keyboard shortcuts root.KeyDown += Root_KeyDown; } private void Root_KeyDown(object sender, KeyRoutedEventArgs e) { switch (e.Key) { case Windows.System.VirtualKey.F: FlipCard(); break; case Windows.System.VirtualKey.Right: NextCard(); break; case Windows.System.VirtualKey.Left: PreviousCard(); break; case Windows.System.VirtualKey.S: Frame.GoBack(); // Stop Studying break; case Windows.System.VirtualKey.E: Frame.Navigate(typeof(EditANotecard)); // Edit this card break; case Windows.System.VirtualKey.D: DeleteNotecardAsync(); break; default: break; } } ... 

The root name is accessed using any keystroke made when this window was opened, using the expression root.KeyDown += Root_KeyDown . This calls the Root_KeyDown() method, which can then implement any function based on sending the key using the keypress argument KeyRoutedEventArgs e .

My code implements a switch to define key functions.

0
source

All Articles