[英]Get Keyboard state in Universal Windows Apps
I want to detect a key combination (eg Control-A
) in a Windows App. 我想检测Windows应用程序中的组合键(例如
Control-A
)。 The KeyDown
event handler has information about the last key pressed. KeyDown
事件处理程序包含有关按下的最后一个键的信息。 But how do I find out whether the Control key is pressed as well? 但是如何确定Control键是否也被按下了?
You can use CoreVirtualKeyStates.HasFlag(CoreVirtualKeyStates.Down)
to determine is the Ctrl key has been pressed, like this - 您可以使用
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
}
};
You can use AcceleratorKeyActivated event, irrespective of where the focus is it will always capture the event. 您可以使用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;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.