arrow keys

  • Thread starter Thread starter Pascal Cloup
  • Start date Start date
One way ou can do this is to override ProcessCmdKey in your form.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
Console.WriteLine("ProcessCmdKey " + keyData.ToString());

return base.ProcessCmdKey(ref msg, keyData);
}

================
Clay Burch
Syncfusion, Inc.
 
Alternatively, I would recommend setting Form.KeyPreview to true and
overriding OnKeyDown on the Form:

protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyData == Keys.Left)
{
// Do something
}
base.OnKeyDown(e);
}
 
Hi,

thanks to all.

In fact, overiding ProcessCmdKey () works but not OnKeyDown() which is not
called for the arrow keys.

best regards,
Pascal

"Deniz Demircioglu" <[email protected]> a écrit
dans le message de (e-mail address removed)...
 
If you set the Form.KeyPreview to true like I said, Form.OnKeyDown would be
called for both arrow and other keys.

One thing I didn't mention is, there is an exception to this rule. If Focus
is on a control deriving from ButtonBase (i.e. Button, RadioButton etc.),
then arrow keys would be used to set the focus to previous (with left and up
arrow keys) and to next (with right and down arrow keys) control. In this
case, Form.OnKeyDown is not called since these keystrokes are interpreted as
Shift+Tab and Tab, respectively.


The approach I'm describing is the preferred way for a container form to
catch some key strokes on its children controls. If you go to documentation
for Control.ProcessCmdKey at msdn:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.processcmdkey.aspx

You will see it says:
"Controls will seldom, if ever, need to override this method."

But if the exception I described above is something you don't want,
overriding ProcessCmdKey may be the only way to go. If that's the case, I
would recommend reading the documentation and returning correct values from
this method as told there.
 
Another solution that has worked for me is to have your form implement
IMessageFilter and call Application.AddMessageFilter(this); to hook up
the filter. This will also catch the arrow keys even when buttons (or
grids, or other things that eat arrows) are present.

=================
Clay Burch
Syncfusion, Inc.
 
Back
Top