Trapping up/down arrow keys

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a class that derives from a Windows forms datagrid and of instance of
this class is hosted within a Windows forms user control. In the derived
class, I have overridden the ProcessDialogKey method and the
PreProcessMessage methods in hopes of trapping a press of the up and down
arrow keys. However, these methods never execute. It seems something else
is intercepting those key strokes, but I am at a loss.

What am I missing?

Thanks for you help.

Chris

Here is the code for the overridden methods:

protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Down || keyData == Keys.Up)
{
MessageBox.Show("ProcessDialogKey: " + keyData.ToString());
}

return base.ProcessDialogKey (keyData);
}

public override bool PreProcessMessage( ref Message msg )
{
Keys keyCode = (Keys)(int)msg.WParam & Keys.KeyCode;
if(msg.Msg == WM_KEYDOWN && (keyCode == Keys.Down || keyCode == Keys.Up) )
{
MessageBox.Show("PreProcessMessage: " + keyCode.ToString());
}
return base.PreProcessMessage(ref msg);
}
 
Chris Kormann said:
I have a class that derives from a Windows forms datagrid and of instance
of
this class is hosted within a Windows forms user control. In the derived
class, I have overridden the ProcessDialogKey method and the
PreProcessMessage methods in hopes of trapping a press of the up and down
arrow keys. However, these methods never execute. It seems something
else
is intercepting those key strokes, but I am at a loss.

What am I missing?

Thanks for you help.

Chris

Here is the code for the overridden methods:

protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Down || keyData == Keys.Up)
{
MessageBox.Show("ProcessDialogKey: " + keyData.ToString());
}

return base.ProcessDialogKey (keyData);
}

public override bool PreProcessMessage( ref Message msg )
{
Keys keyCode = (Keys)(int)msg.WParam & Keys.KeyCode;
if(msg.Msg == WM_KEYDOWN && (keyCode == Keys.Down || keyCode ==
Keys.Up) )
{
MessageBox.Show("PreProcessMessage: " + keyCode.ToString());
}
return base.PreProcessMessage(ref msg);
}


First of all I think it's not good to call MessageBox.Show from there.
Instead you should put there e.g.

protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Down || keyData == Keys.Up)
{
this.SomeMethod();
return true;
}
return base.ProcessDialogKey (keyData);
}

Also, you can try with overriding ProcessCmdKey

HTH
 
Back
Top