Control-click

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.
 
G

Gabriel Lozano-Morán

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
 
G

Guest

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.
 
P

Pipo

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.
 
G

Gabriel Lozano-Morán

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
 
G

Guest

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 :)
 
M

Morten Wennevik

Indeed, though it is not the Form's ModifierKeys, but Form inherits from
Control so ...

There is also the static Control.MouseButtons.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top