在WPF MouseLeftButtonDown事件处理程序中按Ctrl键按条件

时间:2009-12-07 16:40:57

标签: c# wpf event-handling keyboard

如何为某个键盘键添加附加条件到WPF MouseLeftButtonDown事件处理程序?

例如 Ctrl + key

private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{         
    ...
}

3 个答案:

答案 0 :(得分:61)

private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
    if(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) {
        MessageBox.Show("Control key is down");
    } else {
        MessageBox.Show("Control key is up");
    }
}

答案 1 :(得分:42)

如果您只想检测修改器,您还可以使用:

if (Keyboard.Modifiers == ModifierKeys.Control) {}
if (Keyboard.Modifiers == ModifierKeys.Shift) {}

等。更多here

答案 2 :(得分:8)

在.NET 4.0中,您可以使用:

Keyboard.Modifiers.HasFlag(ModifierKeys.Control)