Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

mouseevent

wpf

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.

like image 770
djcouchycouch Avatar asked Aug 14 '09 00:08

djcouchycouch


People also ask

What are Shift alt Ctrl keys called?

Ctrl,shift and alt are called Modifier keys.

Which method is used to capture alt control meta or Shift keys?

Answer: In modern browsers, your script can check whether a keyboard event occurred while the user was pressing any of the Ctrl, Alt, Shift keys. In the example below, this is accomplished for the keydown events. To implement the Ctrl / Alt / Shift detection, you can use the properties event. ctrlKey , event.


2 Answers

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.

like image 151
Andy Avatar answered Oct 11 '22 08:10

Andy


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

like image 21
mkgrunder Avatar answered Oct 11 '22 09:10

mkgrunder