473,796 Members | 2,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9586
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.co m

"Alex K." <Al***@discussi ons.microsoft.c omwrote in message
news:3E******** *************** ***********@mic rosoft.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((ModifierKey s & 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***@newsgrou p.nospamwrote in message
news:OI******** ******@TK2MSFTN GP03.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((ModifierKey s & Keys.Control) == Keys.Control)
{
... do something
}

--
Gregg Walker

May 18 '07 #4
On Fri, 18 May 2007 13:25:23 -0700, Brian Schwartz
<ow***@fishnetc omponentswos.co mwrote:
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 GetAsyncKeyStat e() 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.co m

"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Fri, 18 May 2007 13:25:23 -0700, Brian Schwartz
<ow***@fishnetc omponentswos.co mwrote:
>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 GetAsyncKeyStat e() 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.c omwrote:
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.co m

"Peter Duniho" <Np*********@nn owslpianmk.comw rote 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.c omwrote:
> 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
34344
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
23549
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 VB, I can set keyascii=0 to kill the input. Is there a similar way to do it in JavaScript? Thanks a lot. <HTML><head></head><body> <form name="myForm"> Password: <input type="text"><br> Comfirm: <input type="text" name="myText"
8
2951
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 RightMouseClicks run as if they were CTRL-RightMouseClicks I have to close the form and reopen it to clear this behavior. Then, after the first CTRL-RightMouseClick, it starts all over again.
1
2638
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 last six months or so) for my Linux box. I want to be able to reboot my machine with CTRL-ALT-DEL, but I don't want to do it by mistake. So, I noticed /etc/inittab has these lines
3
44045
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 System.Boolean ProcessCmdKey(ref System.Windows.Forms.Message msg,System.Windows.Forms.Keys keyData)
3
7124
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 which hotkey was pressed? Secondly I am trying to replicate the Ctrl-Tab functionality you have in the DotNet IDE when you hold down the Ctrl key and then additional use the tab key you can cycle thru the MDI Children. I would like to gain the...
2
7021
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
2327
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
4587
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
27394
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 get the event, but if the A key is pressed while the CTRL is pressed – The event is not fired to my KeyDown handler. How do I get the event for the CTRL + A keys? ---------
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10456
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.