简体   繁体   English

Metro应用中的键盘快捷键

[英]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? 那么如何查看页面范围/控件独立的快捷方式?

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. 你可以在这个事件中找到你自己的逻辑。

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. 这是我为简单的KeyDown事件做的事情。

<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">

The best solution was to use the root element as suggested previously, because global shortcuts use the functions of one class applied to every page, meaning that one must handle each shortcut separately on each page or risk errors. 最好的解决方案是使用之前建议的根元素,因为全局快捷方式使用应用于每个页面的一个类的功能,这意味着必须在每个页面上单独处理每个快捷方式或冒险错误。

My XAML was set up as follows: 我的XAML设置如下:

<Page
   Name="root"
    ...>

    ...

</Page>

My C# to manipulate that page is like this: 我操作该页面的C#是这样的:

... 
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 name root is accessed via any keypress made when that window is open, via the expression root.KeyDown += Root_KeyDown . 通过表达式root.KeyDown += Root_KeyDown ,通过窗口打开时所做的任何按键访问名称root That calls the Root_KeyDown() method, which then can implement any function based on the key send by the keypress argument KeyRoutedEventArgs e . 它调用Root_KeyDown()方法,然后该方法可以基于KeyRoutedEventArgs e参数KeyRoutedEventArgs e发送的密钥实现任何功能。

My code implements a switch statement to determine key functions. 我的代码实现了一个switch语句来确定关键函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM