2015-09-25 95 views
9

我想检测Windows应用程序中的组合键(例如Control-A)。 KeyDown事件处理程序包含有关按下的最后一个键的信息。但是我怎样才能知道Control键是否被按下?在通用Windows应用程序中获取键盘状态

+0

正常情况下,你会'KeyPressed == Control | A' – kevintjuh93

+0

@KevinKal谢谢。但我试过了。它只是'A'。 – ispiro

+0

控制是一个系统键,因此它的记录方式不同。我不确定它是如何完成的,但这是你看到的问题 – rmn36

回答

17

您可以使用CoreVirtualKeyStates.HasFlag(CoreVirtualKeyStates.Down)确定是按Ctrl键被按下,这样的 -

Window.Current.CoreWindow.KeyDown += (s, e) => 
{ 
    var ctrl = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control); 
    if (ctrl.HasFlag(CoreVirtualKeyStates.Down) && e.VirtualKey == VirtualKey.A) 
    { 
     // do your stuff 
    } 
}; 
+1

如果我正确理解你的问题,而不是订阅你的页面的'KeyDown'事件,订阅'Window.Current.CoreWindow.KeyDown'就像我的答案。 –

+1

完美!非常感谢! – ispiro

6

您可以使用AcceleratorKeyActivated事件,不管是将重点它将始终捕获事件。

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; 
      } 
     } 
    } 
} 
相关问题