tags:

views:

46

answers:

2

I am developing a Tetris game using C++ in combination with the plain old WinAPI, GDI and GDI+. The applications window contains a few UI controls along with a static control that is used for painting the game state. I am using a keyboard hook so that I can respond to the arrow keys to move the current block and the space bar to drop the block.

However, the keystrokes also affect the UI. Moving the arrow keys may change the radio or combobox selection state and pressing the space bar may trigger a button or checkbox, depending on which control currently has focus.

Is there a way to prevent this from happening? My temporary hack is to force focus on a hidden textbox. I would appreciate a cleaner solution.

+1  A: 

If you're using a WH_KEYBOARD_LL or a WH_KEYBOARD keyboard hook, you can return 1 from the hookproc to prevent the key from being passed to the rest of the system. Do no call the next hookproc in that case.

However, this could play havoc with things that use hooks and come after you in the hook chain.

If you're writing a GDI app, you have a message loop already, is there any reason you're not using the message loop to look for KEY_UP/KEY_DOWN messages?

You can set other controls so they will not take focus...

JimR
Manipulating the events directly from the message loop seems not a bad idea. I'll give it a try.
StackedCrooked
+1  A: 

Is this a real hook (set with SetWindowsHook)? If so, you could return a non zero value from the hookproc when you handle the message, and Windows will not pass the message along to the dialog.

fhj
Yes, it's a WH_KEYBOARD. Thanks.
StackedCrooked