Stuby085

I'm in the middle of writing a PacMan clone using GDI+. I've currently got code to create the maze, the dots, and the PacMan figure. I even have movement working. That is, until I tried to change the main window from a Form to a UserControl. Now I can draw everything, but the control doesn't accept the keydown event. I've properly created a handler, that isn't the problem. I believe that the problem is that Form containing the UserControl is capturing the event instead of the UserControl. How do I make the UserControl catch the event instead


Re: Windows Forms General Custom UserControl not accepting Key events

Friendly Dog

Did you try to click on your control first then try if the keydown is working




Re: Windows Forms General Custom UserControl not accepting Key events

Stuby085

Yes, clicking the control first and then typing doesn't do any thing for me either.

*Edit*
I've narrowed the problem further, the event is called, just not when the arrow keys are pressed...  Using the arrow keys in a KeyDown event works just fine with Forms, why wouldn't it with UserControls




Re: Windows Forms General Custom UserControl not accepting Key events

Stuby085

I solved the problem by overriding the UserControl's IsInputKey() method. I'll post my code as an example to anyone else with my problem.

protected override bool IsInputKey(Keys key)
{
if (key.Equals(m_Up) || key.Equals(m_Left)
|| key.Equals(m_Down) || key.Equals(m_Right))
return true;

return base.IsInputKey(key);
}