Control-click

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When a control's 'Click' event is triggered, is there a way I can test
whether the CTRL / Alt / Shift keys are pressed as well? I've thought about
capturing KeyDown events before the click but this assumes that the control
is in focus which won't necessarily be so.
 
You can capture this in the KeyDown() and KeyUp() event

Using either e.Modifiers or checking if e.Control || e.Shift || e.Alt equals
to true

Gabriel Lozano-Morán
 
But as I said, the control won't necessarily be in focus when the CTRL / Alt
/ Shift key is pressed which means the KeyDown / KeyUp events won't be
triggered.
 
Barguast,

You can capture the keyevents if you set the keypreview to true on the form.
That way when ever the form has the focus, you could get the Keys pressed.
 
Sorry i totally misunderstood the question. Hope this can help you get
started:

[System.Runtime.InteropServices.DllImport("User32",
EntryPoint="GetKeyState", ExactSpelling=false,
CharSet=System.Runtime.InteropServices.CharSet.Ansi, SetLastError=true)]
private static extern int GetKeyState(long nVirtKey);
private const int VK_CONTROL = 0x11;
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(GetKeyState(VK_CONTROL).ToString());
}

Gabriel Lozano-Morán
 
Thanks for yours replies, but I've just found out about the static
'System.Windows.Forms.ModifierKeys' property which can be checked during the
click event. For example:

private void Cell_Click (object sender, EventArgs e)
{
if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
{
// The control has been CTRL-Clicked
}
}

That'll do the trick :)
 
Indeed, though it is not the Form's ModifierKeys, but Form inherits from
Control so ...

There is also the static Control.MouseButtons.
 
Back
Top