How to capture keystroke

  • Thread starter Thread starter Larry Smith
  • Start date Start date
L

Larry Smith

(Sorry for multi-posting this in "microsoft.public.dotnet.languages.csharp")

Hi there,

Can someone enlighten me on how to trap a keystroke in my "PropertyGrid"
override. I see functions or properrites at the control or form level
called:

OnKeyDown
OnKeyPress
OnKeyUp
OnPreviewKeyDown
ProcessKeyPreview
ProcessDialogKey
ProcessCmdKey
KeyPreview
IsInputKey

and there are probably more. I've overrided "OnKeyDown()" in my derived
"PropertyGrid" class but it's not being called. Some others are called
however such as "ProcessCmdKey" but I need to understand what's going on
here (since I suspect that "OnKeyDown()" is really the correct function).
Can anyone point me in the right direction. Thanks.
 
Your strategy will depend on why you need to trap the keystrokes. There
may be another way to do it.
 
Your strategy will depend on why you need to trap the keystrokes. There
may be another way to do it.

Thanks for the feedback. I'm trying to intercept <Tab> and <Enter> so I can
convert them to the down arrow key (or convert <Shift-Tab> to up arrow). I
can pull this off using "ProcessCmdKey()" (it works though there's another
issue I might post separately) but I want to understand why "OnKeyDown()"
isn't even being called.
 
Larry said:
Thanks for the feedback. I'm trying to intercept <Tab> and <Enter> so I can
convert them to the down arrow key (or convert <Shift-Tab> to up arrow). I
can pull this off using "ProcessCmdKey()" (it works though there's another
issue I might post separately) but I want to understand why "OnKeyDown()"
isn't even being called.

If that's the method in the form, it's probably not being called because
you've not set KeyPreview to true.

Alun Harford
 
Your strategy will depend on why you need to trap the keystrokes. There
If that's the method in the form, it's probably not being called because
you've not set KeyPreview to true.

I'm overriding "OnKeyDown()" in a "PropertyGrid" derivative. "KeyPreview" is
off in the form itself.
 
In that case, I would override the WndProc method and filter for the
WM_KEYDOWN event.

Then, modify the message parameters to be down or up based on the
original parameters.

That way it will work no matter what because you are intercepting the
windows message before your control has a chance to process it.

--
Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
Web Site: http://www.composablesystems.net
 
Back
Top