Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / September 2006

Tip: Looking for answers? Try searching our database.

KeyDown not capturing all keys

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MLM450@hotmail.com - 18 Sep 2006 16:08 GMT
I have a control that handles the KeyDown event but it does not seem to
execute when a combination of keys is pressed - like CTRL+Z. If I
press CTRL, it executes. If I press Z, it executes. But the handler
does not see the combination.

Now this control is contained within another control which is contained
within another. The top most control does see the CTRL+Z. I can easily
pass down the key info, but why does the nested control see some keys
events and not others? It would be cleaner if the nested control simply
handled all input.

Thanks,
Mike
Kevin Spencer - 18 Sep 2006 20:02 GMT
There are several different ways of handling keyboard input in Windows
Forms. The KeyDown event captures many keys, but not all. I suggest reading
the following:

http://msdn2.microsoft.com/en-us/library/ms171535.aspx

Signature

HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

>I have a control that handles the KeyDown event but it does not seem to
> execute when a combination of keys is pressed - like CTRL+Z. If I
[quoted text clipped - 9 lines]
> Thanks,
> Mike
MLM450@hotmail.com - 19 Sep 2006 12:20 GMT
None of this is making a difference. I found that the control works
fine when it is used within a C# application. The problem occurs when
it is used within a C++ application. It seems that simple keys like
"Z" are routed to the sub-control that has focus while the "CTRL+Z"
key strokes are routed to the topmost control. Strange...

> There are several different ways of handling keyboard input in Windows
> Forms. The KeyDown event captures many keys, but not all. I suggest reading
[quoted text clipped - 24 lines]
> > Thanks,
> > Mike
Kevin Spencer - 19 Sep 2006 13:49 GMT
Noe of what is making a difference? If you read the article, you would
notice that it mentions several specific alternative methods used to capture
keyboard input, and details which methods should be used to capture
different kinds of keyboard input.

In particular, I have used the ProcessCmdKey method to capture any keyboard
input at the Form level before it is processed any further. This method
captures *all* keyboard input, and the return value of it determines whether
the key willl be further processed (works like CancelEventArgs). See
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.processcmd
key.aspx

for more information on this method, and be sure and fully read the article
I pointed you to, as it details which methods to use in different
circumstances.

Signature

HTH,

Kevin Spencer
Microsoft MVP
Digital Carpenter

A man, a plan, a canal,
a palindrome that has gone to s**t.

--
HTH,

Kevin Spencer
Microsoft MVP
Digital Carpenter

A man, a plan, a canal,
a palindrome that has gone to s**t.

> None of this is making a difference. I found that the control works
> fine when it is used within a C# application. The problem occurs when
[quoted text clipped - 31 lines]
>> > Thanks,
>> > Mike
MLM450@hotmail.com - 19 Sep 2006 14:58 GMT
Well, I don't know where your attitude came from, but I don't
appreciate it. How nice of you to assume that I ignored the article. I
admit that I did not read every syllable, but I did look it over and
tried some of what it showed. That is what did not work. However, I
will look it over again to be sure I didn't miss something important.

ProcessCmdKey doesn't work either. Perhaps because I am using the
control in a CWinFormsControl, I am not sure.

> Noe of what is making a difference? If you read the article, you would
> notice that it mentions several specific alternative methods used to capture
[quoted text clipped - 65 lines]
> >> > Thanks,
> >> > Mike
Kevin Spencer - 19 Sep 2006 21:21 GMT
CWinFormsControl is not a managed class. It is MFC (C++). It will not have
the same properties or methods as a System.Windows.Forms.Form class
instance. As this is a Managed newsgroup, I naturally assumed that you were
working with a managed Form. At this point, I have no idea what (else)
you're working with, or how your app is designed. It sounds like perhaps
some mixture of managed and unmanaged code, but that's just a guess, and
beyond that is even more of a guess. Therefore, I can't help any further.

Signature

HTH,

Kevin Spencer
Microsoft MVP
Digital Carpenter

A man, a plan, a canal,
a palindrome that has gone to s**t.

> Well, I don't know where your attitude came from, but I don't
> appreciate it. How nice of you to assume that I ignored the article. I
[quoted text clipped - 83 lines]
>> >> > Thanks,
>> >> > Mike
Morten Wennevik - 19 Sep 2006 08:19 GMT
Hi Mike,

You can test for modifierkeys in a KeyDown event

            if (e.KeyCode == Keys.Z && (e.Modifiers & Keys.Control) > 0)
                // CTRL + Z

> I have a control that handles the KeyDown event but it does not seem to
> execute when a combination of keys is pressed - like CTRL+Z. If I
[quoted text clipped - 9 lines]
> Thanks,
> Mike

Signature

Happy Coding!
Morten Wennevik [C# MVP]


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.