Getting shift/ctrl/alt states from a mouse event?

会有一股神秘感。 提交于 2020-01-21 06:18:25

问题


In my WPF App, how do I get the state of the shift, ctrl and alt keys in my mouse event handler? I seem to remember in MFC you could get that information from the mouse event.


回答1:


Assuming that you're still in the mouse event handler, you can check the value of 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.




回答2:


As per Andy's answer, you use 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



来源:https://stackoverflow.com/questions/1275371/getting-shift-ctrl-alt-states-from-a-mouse-event

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!