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

When I first press the control key (left) and then press the left mouse button why the following code is executed. I am modifying existing code and the code below already exists. I assume that no one has tried this before by pressing the control key, it was used only with a left click and always worked for this case. But I want another code to be executed when the mouse button is pressed, pressed at the same time as pressing the control key.

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 really appreciate any hint or help.

PS I would like to add that in the above case, when I check the value of e.button, it shows that it is equal to Right, although I pressed the left mouse button and the Ctrl key. This is a mystery to me.

Dear StackOverflow fellows: I found a problem, since I use VM on MAC, I had to disable some key mappings in the settings of my virtual machine, and now my source code is working. Thank you for your help.

+4
source share
2 answers

The problem was that between the MAC and my Windows virtual machine, a keyword was matched that needed to be disabled. thanks for the help

0
source

Keys.None is set to 0, which makes it difficult to detect when a "key is not pressed" when used alone. It:

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

A message box will appear, any key combination if a 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."); } } 

Only a message will be displayed when the Control key is held down (and the left mouse button is pressed).

Try changing the conditions and detect when the Control key is pressed, when you press (instead of detecting when no key is pressed). It is said that it is difficult for me to work with the same code with Keys.ControlKey or Keys.LControlKey for some reason, so a little more research is required to select the left control key.

+9
source

All Articles