Detecting Ctrl+Left (mouse button) in MouseDown event handler

19,446

Solution 1

The problem was that there was a key mapping between MAC and my Windows Virtual Machine that needed to be disabled. Thanks for all the help

Solution 2

Keys.None has a value of 0, making it hard to detect when "no key is pressed" when used alone. This:

    void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && (ModifierKeys & Keys.None) == Keys.None)
        {
            MessageBox.Show("No key was held down.");
        }
    }

Will pop a message box whatever the key combination is, as long as the click occurs with the left button.

However, this:

    void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && (ModifierKeys & Keys.Control) == Keys.Control)
        {
            MessageBox.Show("Control key was held down.");
        }
    }

Will only pop a message box when the Control key is held down (and the left mouse button is clicked).

Try reversing your conditions and detect when the Control key is pressed when clicking (instead of detecting when no key is pressed). That being said I'm having a hard time getting the same code to work with Keys.ControlKey or Keys.LControlKey for some reason, so isolating the left control key needs a little more research.

Share:
19,446
user1298925
Author by

user1298925

Updated on June 28, 2022

Comments

  • user1298925
    user1298925 almost 2 years

    When I first press down control key (the left one) and then click the left mouse button, why does the following code gets executed. I am modifying existing code and the below code is already there. I guess no one has tried it before, with control key pressed, it has only been used with left-mouse-clicked and it has always worked for that case. But I want a different code executed when the mouse left key is pressed at the same time that the control key is pressed.

    private void treeList1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        TreeList tree = sender as TreeList;
    
        if (e.Button == MouseButtons.Right && ModifierKeys == Keys.None && tree.State == TreeListState.Regular)
        {
           //the code that is here gets executed 
           MessageBox.Show("I am here");
        }
    }
    

    I would highly appreciate any hint or help.

    P.S. I would like to add that in the above case when I inspect e.button value it shows that is equal to Right although I pressed the left mouse button and the Ctrl Key. That is a mystery to me.

    Dear StackOverflow fellows :I found the problem, since I am using a VM on a MAC I had to disable some Key Mapping on my Virtual Machine preference and now my original code works. Thanks for all your help.

  • user1298925
    user1298925 about 11 years
    I tried your first box of code, it does not work. Can you please edit what is needed after e. Maybe I am filling the blanks that you have with wrong value. So, I still dont have any answer. Still when I click the left mouse button with the ctrl down it executes the code for right mouse click! I have not tried your second box yet. Thanks.
  • Mathieu Guindon
    Mathieu Guindon about 11 years
    Interesting. Have you reversed the condition or trapping Keys.None worked?
  • user1298925
    user1298925 about 11 years
    Have not tried that yet, I will and I will definitely let you know. Thanks again.
  • V. Panchenko
    V. Panchenko over 8 years
    I tried this code (second code-block) - it works! Thank you, @Mat'sMug! :)
  • Emperor Orionii
    Emperor Orionii almost 5 years
    I'm a bit late but since .Net 4.0 (at least) enums have HasFlag method for checking if certain bits are set.