Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if Shift is being pressed in a MouseDown event?

I am catching a MouseDown event on a control, which gives me a MouseEventArgs object in the signature. Now I want to be able to tell if the user was holding down the "Shift" or "Control" key when they clicked. But the MouseEventArgs object doesn't contain any keyboard information!

What's the easiest way of telling whether the keyboard Shift/Ctrl keys were being held at the time of the click?

like image 675
Shaul Behr Avatar asked Sep 13 '10 15:09

Shaul Behr


2 Answers

Use the Control.ModifierKeys property to see what's pressed. For example:

    private void Form1_MouseClick(object sender, MouseEventArgs e) {
        if (Control.ModifierKeys == Keys.Control) {
            Console.WriteLine("Ctrl+Click");
        }
    }

Other modifiers are Keys.Alt and Keys.Shift. Find combinations with, say, (Keys.Control | Keys.Shift).

like image 132
Hans Passant Avatar answered Oct 16 '22 02:10

Hans Passant


In C#, you can check using -

Keyboard.IsKeyDown(Key.LeftShift) or key.RightShift

http://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.iskeydown.aspx

http://msdn.microsoft.com/en-us/library/system.windows.input.key.aspx

like image 24
Sachin Shanbhag Avatar answered Oct 16 '22 03:10

Sachin Shanbhag