0

I have a form that creates a class. This class processes events that are fired on the form. The problem is I am trying to use the KeyDown event, but it isn't working because there are buttons on the form and they are capturing the KeyDown. I found the solution on another post was to override the ProcessCmdKey. The problem is I don't know how to override a method from inside another class. Can anyone tell me how I can capture all KeyDown events from inside my other class?

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.Left)
    {
        MoveLeft(); DrawGame(); DoWhatever();
        return true; //for the active control to see the keypress, return false
    }
    else if (keyData == Keys.Right)
    {
        MoveRight(); DrawGame(); DoWhatever();
        return true; //for the active control to see the keypress, return false
    }
    else if (keyData == Keys.Up)
    {
        MoveUp(); DrawGame(); DoWhatever();
        return true; //for the active control to see the keypress, return false
    }
    else if (keyData == Keys.Down)
    {
        MoveDown(); DrawGame(); DoWhatever();
        return true; //for the active control to see the keypress, return false
    }
    else
        return base.ProcessCmdKey(ref msg, keyData);
}
Synaps3
  • 1,597
  • 2
  • 15
  • 23

2 Answers2

0

The easiest way to do this would be to expose the KeyDown from Button on the containing form.

class MyForm : Form { 
  Button m_button;

  public event KeyEventHandler ButtonKeyDown;

  public MyForm() { 
    m_button = ...;
    m_button.KeyDown += delegate (object, e) {
      KeyEventHandler saved = ButtonKeyDown;
      if (saved != null) { 
         saved(object, e);
      }
    };
  }
}

Now the calling code can simple hook into the MyForm::ButtonKeyDown event

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

I'm not sure how you're wiring up the events with your class, but if you set the KeyPreview property of the form to True, you can grab a hold of the event there and then pass it along to your class that is processing the events. So even when the button has the focus, the KeyDown will fire the event on the form.

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    ... Invoke your class
}
Glenn Cuevas
  • 166
  • 5
  • Setting the KeyPreview property does work. The only problem is certain keys still don't fire (arrow keys, tab). How can I make this work for all keys? – Synaps3 Jul 24 '13 at 22:21
  • I really still need help with detecting arrow keys, tab. – Synaps3 Jul 24 '13 at 22:51
  • Check out [link](http://stackoverflow.com/questions/1646998/up-down-left-and-right-arrow-keys-do-not-trigger-keydown-event) they have a solution for arrow keys. – Glenn Cuevas Jul 24 '13 at 23:56
  • I know about the ProcessCmdKey. That's what my posted code above does. My question is how can I make that work from within another class without touching the form's class. – Synaps3 Jul 25 '13 at 01:17
  • Since the form and it's controls are where the events are going to originate from, some wiring needs to be done to delegate the handling of those events. You may need to post up a larger sampling of what you're trying to do, that may give us a better picture. – Glenn Cuevas Jul 25 '13 at 21:55
  • I just settled with using ProcessCmdKey in the main form class. Inside of it I called the appropriate method inside my class and passed the key that was pressed into it. It is not optimal because I would like to keep as much of this construction code out of my main form as possible, but it will do. The idea is, I want my class to be as reusable as possible without people having to put construction code outside of it. Thanks for the help! – Synaps3 Jul 28 '13 at 01:25