KeyDown event

M

Mike M

I am trying to detect the keydown of the arrow keys in a
custom control that I am writing. As far as I can tell,
pressing the arrow keys does not fire the KeyDown event
at all. It behaves just like the tab key, passing focus
to the next control in the tab order. The only events
that fire are in the lost focus sequence (leave,
validating, validated, etc.) Any ideas?
 
N

Nicholas Paldino [.NET/C# MVP]

Mike,

Are you attaching to the KeyDown event, or are you overriding the
OnKeyDown method? You might be able to get it in the OnKeyDown method, as
filtering might occur on that level.

If it does not, then you can override the WndProc method of the class
and handle the windows message for the key down event WM_KEYDOWN.

Hope this helps.
 
M

Mike M

I have tried both attaching to the event and overriding
OnKeyDown. Neither one worked. I was hoping that maybe
the Control class' OnKeyDown was maybe filtering out and
interpreting the arrow key, but if that was the case then
overriding (and waiting to call the base class'
OnKeyDown) should have fixed the problem.

I'll try the WndProc method, though. Thanks.
-----Original Message-----
Mike,

Are you attaching to the KeyDown event, or are you overriding the
OnKeyDown method? You might be able to get it in the OnKeyDown method, as
filtering might occur on that level.

If it does not, then you can override the WndProc method of the class
and handle the windows message for the key down event WM_KEYDOWN.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mike M said:
I am trying to detect the keydown of the arrow keys in a
custom control that I am writing. As far as I can tell,
pressing the arrow keys does not fire the KeyDown event
at all. It behaves just like the tab key, passing focus
to the next control in the tab order. The only events
that fire are in the lost focus sequence (leave,
validating, validated, etc.) Any ideas?


.
 
A

A

Nicholas Paldino said:
Mike,

Are you attaching to the KeyDown event, or are you overriding the
OnKeyDown method? You might be able to get it in the OnKeyDown method, as
filtering might occur on that level.

If it does not, then you can override the WndProc method of the class
and handle the windows message for the key down event WM_KEYDOWN.

Actuall, I tihnk what you want is to check out the IsInputKey method. IIRC,
there are a couple of methods that determine how certain keys are processed,
and you can override the default and do it yourself, so that the special
keys can be treated as regular keys as opposed to their default as "special
keys." I know there is a way to do this without having to resort to the
WM_KEYDOWN override in WndProc.

-akshay
 
M

Mike M

As it turns out, it is the ProcessCmdKey method. Thanks
for all your help, though. It got me going in the right
direction. Here's an example...

protected override bool ProcessCmdKey(ref Message m, Keys
keyData)
{
bool blnProcess = false;

if (keyData == Keys.Right || Keys.Down)
{
// Process the keystroke
blnProcess = true;
}
else if (keyData == Keys.Left || Keys.Up)
{
// Process the keystroke
blnProcess = true;
}

if (blnProcess == true)
return true;
else
return base.ProcessCmdKey(ref m, keyData);
}
 

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