WM_KEYDOWN event better explanation


2004-03-05 05:02:59 PM
off-topic9
What I really need is to redirect the input from
the menu item to the OnKeyDown event handler.
I explain it shortly what I mean with it.
I need to process keyboard input uniformly for input
from the menu items and for any key combination
that is no menu item hot key.
I have decided to place the processing function inside
from OnKeyDown event handler. The key combinations
that are no menu hot keys will land in that event
without my work. But the menu hot keys will not.
I must make them to do it.
I have decided simply to send WM_KEYDOWN
messege from menu item event handle with the same
hot key as that from menu item.
It works for <F1>: SendMessage( handle, WM_KEYDOWN, VK_F1, 0 );
It works for <Alt+F1>: SendMessage( handle, WM_KEYDOWN, VK_F1, 0x20000000 );
But it does not work for <Ctrl+F1>
I have found the solution:
BYTE KeyState[256] = {0};
GetKeyboardState( KeyState );
BYTE old_state = KeyState[VK_CONTROL];
KeyState[VK_CONTROL] |= 0x80;
SetKeyboardState( KeyState );
SendMessage( handle, WM_KEYDOWN, key, 0 );
KeyState[VK_CONTROL] = (BYTE)(old_state & (~(0x80)));
SetKeyboardState( KeyState );
It works, it will land in OnKeyDown event hanlder with <Ctrl+F1>
but the second SetKeyboardState( KeyState ) comes too late.
For the whole action that was by SendMessage() activated is the <Ctrl>pressed.
When I'm for example editing some dialog it does not work, I must press <Ctrl>at first.
I need a solution that does not let <Ctrl>active.