Can WM_HOTKEY be used to simulate hotkey events?
In my program(C++, WinAPI), I wanted to simulate pressing some existing hotkeys set in other running programs. I know there is a SendInpu开发者_StackOverflowt function which simulates the keyboard input, but it seemed too much work as it needs to create a lot of structures for the keys.
I was trying use SendMessage or PostMessage with HWND_BROADCAST and WM_HOTKEY parameters. Neither worked.
The code looks like this:
WORD hotkey = MAKEWORD(MOD_CONTROL, VK_SPACE); // Ctrl + Space
SendMessage( HWND_BROADCAST,
WM_HOTKEY,
(WPARAM)hotkey , 0);
Am I on the right track? or this should be done completely in a different way?
SendInput is the best answer, honestly. This is because in the message loop your calls to the traslateaccellerator will look at the message , and in that message theres a bit flag on LPARAM, indicating what other keys are pressed with that key. You dont want to have to fill out that bit flag yourself, but ..... just in case you do .. and dont want to use SendInput...http://msdn.microsoft.com/en-us/library/ms646280%28VS.85%29.aspx details how to do it. Of course, if you want to use an AcceleratorTable, thats easily done with a resource editor, just in your message loop, hook it up with this http://msdn.microsoft.com/en-us/library/ms646373%28VS.85%29.aspx
Do not use HWND_BROADCAST with something like WM_HOTKEY. In fact, avoid HWND_BROADCAST except in the circumstances when you really do want to broadcast a message to every single window. You are going to hit an unbelievable amount of trouble with other windows processing your message otherwise.
You are also using WM_HOTKEY wrong. wParam does not compose of a HIWORD/LOWORD of two keys. Programmers assign hotkeys with RegisterHotKey which they pass an identifier and also the hotkeys which generate a WM_HOTKEY message with that identifier.
There are several ways for you to find this identifier which should be what your wParam is. First off you could debug the target and breakpoint on RegisterHotKey() till you find the one you want. Alternatively you could use Spy++ and target the window and check what the identifier is.
There is of course the possibility the hotkey is not working via a hotkey but instead something like a keyboard accelerator. You would need to follow similar steps to find the parameters are to the WM_COMMAND or WM_SYSCOMMAND that is received by the target when you press that button. My recommendation is to go with Spy++.
精彩评论