473,320 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Is Ctrl key pressed?

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
May 18 '07 #1
7 9525
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alex K." <Al***@discussions.microsoft.comwrote in message
news:3E**********************************@microsof t.com...
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

May 18 '07 #2
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
}

--
Gregg Walker
May 18 '07 #3
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.

--
Brian Schwartz
FishNet Components
http://www.fishnetcomponents.com
Fish Grid .NET Light: Powerful Layouts for Small Datasets
"Gregg Walker" <xy***@newsgroup.nospamwrote in message
news:OI**************@TK2MSFTNGP03.phx.gbl...
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
}

--
Gregg Walker

May 18 '07 #4
On Fri, 18 May 2007 13:25:23 -0700, Brian Schwartz
<ow***@fishnetcomponentswos.comwrote:
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
May 19 '07 #5
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 18 May 2007 13:25:23 -0700, Brian Schwartz
<ow***@fishnetcomponentswos.comwrote:
>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
May 19 '07 #6
On Fri, 18 May 2007 18:35:38 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
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
May 19 '07 #7
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 18 May 2007 18:35:38 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
> 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
May 19 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mr. x | last post by:
Hello, I want to capture the event when the <ctrl> key is pressed. How can I do that ? Thanks :)
10
by: chirs | last post by:
I have a code to disable ctrl-v (paste) on the 2nd box. The problem is that when I type ctrl-v, the text shows, then disappear after I release ctrl-v. How can I make it not to show in the box. In...
8
by: MLH | last post by:
I use a mouse-down procedure to trap right mouse clicks and CTRL-Right mouse clicks. Running the procedure must put honey or some other sticky substance into my keyboard because subsequent...
1
by: Pedro Graca | last post by:
Well ... I've been using Windows for a long time before I switched to Linux. I'm very used to type CTRL-ALT-DEL everytime I'm leaving my computer or arriving to it. It's hapenned _twice_ (in the...
3
by: Stefan | last post by:
Hy, i have an app and i must disable this combination: ALT+F4; CTRL+ALT+DEL; CTRL+ESC;ALT+TAB like this: i find something on Internet and i can block ALT+F4 protected override...
3
by: Glen Hong | last post by:
I have set up a hotkey using RegisterHotKey API function for Ctrl-Tab. Firstly, I can rap this when Ctrl-Tab is pressed however if I want to add an addition hotkey how can I distinguish between...
2
by: Du | last post by:
How to tell if the Ctrl key is pressed without being inside a keypress/keyup/keydown event? I want distinguish between a "click" and a "ctrl+click" on a button. any suggestion? thanks
6
by: Alex K. | last post by:
My MDI form has several child forms in maximized state. Pressing Ctrl-F6 causes next child to show, which I don't like. How can I cancel standard Ctrl-F6 behaviour? Thank you
4
by: Tom | last post by:
Hi, I would like to know wether the Ctrl-key is pressed down when i click a button on my win form. how can i do this? tia, Tom
5
by: Sharon | last post by:
I’m using the DataGrid control. The DataGrid allow select all by clicking on CTRL+A keys. I need to catch this keys event, so I tried using the KeyDown event, but only when the CTRL is pressed I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.