简体   繁体   English

从鼠标事件中获取shift / ctrl / alt状态?

[英]Getting shift/ctrl/alt states from a mouse event?

In my WPF App, how do I get the state of the shift , ctrl and alt keys in my mouse event handler? 在我的WPF应用程序中,如何在鼠标事件处理程序中获取shiftctrlalt键的状态? I seem to remember in MFC you could get that information from the mouse event. 我似乎记得在MFC你可以从鼠标事件中获取该信息。

Assuming that you're still in the mouse event handler, you can check the value of Keyboard.Modifiers . 假设您仍在鼠标事件处理程序中,则可以检查Keyboard.Modifiers的值。 I don't think that there is anyway to get modifier information from the event itself, so you have to interrogate the keyboard directly. 我不认为无论如何都要从事件本身获取修改器信息,因此您必须直接询问键盘。

As per Andy's answer, you use Keyboard.Modifiers. 根据Andy的回答,你使用Keyboard.Modifiers。 I figured I would post a little example 我想我会发一个小例子

Something like this in your event handler should work: 你的事件处理程序中的这样的东西应该工作:

private void MyExampleButton_Click(object sender, RoutedEventArgs e)
{
    if ((Keyboard.Modifiers & ModifierKeys.Control) > 0) {
        System.Diagnostics.Debug.WriteLine("Control is pressed");
    } else {
        System.Diagnostics.Debug.WriteLine("Control is NOT pressed");
    }
}

Regards, Mike 问候,迈克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM