Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何确定哪个键触发了RoutedCommand事件_C#_Wpf - Fatal编程技术网

C# 如何确定哪个键触发了RoutedCommand事件

C# 如何确定哪个键触发了RoutedCommand事件,c#,wpf,C#,Wpf,我想在我的应用程序中支持几个快捷键。如果我使用这个结构,请参阅所附的代码,即在一个输入手势中插入更多手势,它是有效的-两个键盘快捷键都调用该功能,但我如何识别它是哪种可能的组合键 RoutedCommand myCmd = new RoutedCommand(); myCmd.InputGestures.Add(new KeyGesture(Key.A, ModifierKeys.Control)); myCmd.InputGestures.Add(new KeyGesture(Key.B, M

我想在我的应用程序中支持几个快捷键。如果我使用这个结构,请参阅所附的代码,即在一个输入手势中插入更多手势,它是有效的-两个键盘快捷键都调用该功能,但我如何识别它是哪种可能的组合键

RoutedCommand myCmd = new RoutedCommand();
myCmd.InputGestures.Add(new KeyGesture(Key.A, ModifierKeys.Control));
myCmd.InputGestures.Add(new KeyGesture(Key.B, ModifierKeys.Control));
CommandBindings.Add(new CommandBinding(myCmd, CtrlKeyPressed));

void CtrlKeyPressed(object sender, ExecutedRoutedEventArgs e)
{ 
   if(Key.A ..do something ?

谢谢

您可以使用
键盘.IsKeyDown
方法来确定按下了哪些键:

private void CtrlKeyPressed(object sender, ExecutedRoutedEventArgs e)
{
    if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
    {
        if (Keyboard.IsKeyDown(Key.A))
        {
            MessageBox.Show("CTRL+A");
        }
        else if (Keyboard.IsKeyDown(Key.B))
        {
            MessageBox.Show("CTRL+B");
        }
    }
}

或者简单地添加两个不同的命令绑定。

谢谢,我希望这些信息可以在ExecutedRoutedEventArgs中找到,但我在那里找不到。然而,你的两个建议看起来都不错。