Is Ctrl key pressed?

G

Guest

Hi all

When processing datagrid's MouseDown event, I need to know if Ctrl key is
currently pressed. Is there any way to check this?
It would help if I had keyUp and KeyDown events for Ctrl key ...

Thank you
 
N

Nicholas Paldino [.NET/C# MVP]

Alex,

You could hook up to the KeyDown and KeyUp events, and check the Control
parameter of the KeyEventArgs instance that is passed to those methods. You
can then store that value in a flag that is accessible to your mouse click
handler. In your handler, you would check if the value is set. If it is,
you know the user is holding down the control key.
 
G

Gregg Walker

Alex - You may simply use the ModifierKeys property of your form to check if
the Ctrl Key is pressed down.

i.e. ...

if((ModifierKeys & Keys.Control) == Keys.Control)
{
.... do something
}
 
B

Brian Schwartz

Yes, this will do it. I recommend grabbing a copy of ModifierKeys as soon as
the procedure starts, and then checking the copy throughout your procedure
as needed. On the off chance that your procedure is long-running, this will
avoid a situation where the user has long since let go of the key, even
though it was pressed at the actual time of the click.
 
P

Peter Duniho

Yes, this will do it. I recommend grabbing a copy of ModifierKeys as
soon as the procedure starts, and then checking the copy throughout your
procedure as needed. On the off chance that your procedure is
long-running,
this will avoid a situation where the user has long since let go of the
key,
even though it was pressed at the actual time of the click.

Are you sure that's a concern? The docs are not clear about this, and
there are multiple ways in the underlying native Windows API to get the
information. Only if it's effectively calling GetAsyncKeyState() would it
be necessary to get a copy as soon as you enter the event handler.

I haven't been able to prove to myself that it's *not* necessary, but I'm
curious if you know for sure that it is. It seems to me that given the
design of .NET, it would make more sense for the ModifierKeys property to
act more like GetKeyState() or even return the state of the flags passed
to the mouse down messages (WM_LBUTTONDOWN, etc.)

Thanks,
Pete
 
N

Nicholas Paldino [.NET/C# MVP]

This is why I recommended setting the state when the control button was
pressed down, and changing that state when it was released. This way, you
don't have that concern.
 
P

Peter Duniho

This is why I recommended setting the state when the control button
was pressed down, and changing that state when it was released. This
way, you don't have that concern.

That's fine. However, it seems to me that the behavior of the
ModifierKeys property should be well-defined. In fact, I suspect that it
*is* well-defined, and just poorly documented.

I'm asking if anyone knows what the well-defined behavior actually is.

It's well and good to write code defensively, especially in absence of
good documentation, but you shouldn't have to in this case, and I'd like
to know what the answer is.

Pete
 
N

Nicholas Paldino [.NET/C# MVP]

Peter,

The ModifierKeys property makes a call to GetKeyState, so you can call
it repeatedly throughout an event handler and get the same result.

Of course, this is an implementation detail, so counting on it would be
a bad idea.
 

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