C# WPF MouseLeftButtonDown 事件处理程序中的 Ctrl 键按下条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1861195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:27:26  来源:igfitidea点击:

Ctrl key press condition in WPF MouseLeftButtonDown event-handler

c#wpfevent-handlingkeyboard

提问by rem

How I can add an additional condition for a certain keyboard key, to a WPF MouseLeftButtonDownevent-handler?

如何将某个键盘键的附加条件添加到 WPFMouseLeftButtonDown事件处理程序?

For example Ctrl+ key

例如Ctrl+键

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

采纳答案by Stanislav Kniazev

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");
    }
}

回答by 742

If you want to detect modifiers only, you can also use:

如果只想检测修饰符,还可以使用:

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

etc. More here.

等更多在这里

回答by Grzegorz Godlewski

In .NET 4.0 you could use:

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

Keyboard.Modifiers.HasFlag(ModifierKeys.Control)