I’m currently working on a simple game that is drawn on a form by overriding the OnPaint method. The game requires Keyboard input and was working perfectly until I decided to enhance the GUI and add a few Buttons to the form.
The moment I added these Buttons, the form stopped receiving any Keyboard input, no matter how hard I tried the focus was always on the buttons. This behavior can be replicated by placing any Focus-able Control on the form. (ie. TextBox)
I don’t need ANY Kayboard interaction with these buttons, I want the user to interact with them with the mouse only.
I’ve tried the following techniques to try and get around this problem – none of these worked:
- 1) Normal
KeyDownandKeyUpevents of the form. (This is the way
I was capturing Keyboard input before placing the buttons.) - 2) Overriding the Form’s
OnKeyDownandOnKeyUpevents. - 3) Overriding
ProcessCmdKey– Works, but cannot differentiate
between KeyUp and KeyDown events, so it is inadequate for me.
I also tried create a MessageFilter for the application, but I couldn’t force it to capture only the Keyboard keys that I needed.
I’ve been looking into this for many hours already and can’t find a suitable solution.
Help would be greatly appreciated.
Here is a sample form with a
IMessageFilterfor the up and down arrow keys, hope this helps:For a list of possible Windows messages check:
List Of Windows Messages