Why doesn’t my keyboard hook get called for keyboard messages I manually posted?
A customer was generating synthesized keyboard input by posting
WM_KEYDOWN and related messages to a window. They found that a
WH_KEYBOARD keyboard hook was not triggering for these keyboard messages and they wanted to know why.
You already know enough to answer this question, though the pieces are scattered about over several years for you to put together.
First of all, you can’t simulate keyboard input with
PostMessage because you are going straight to the end product without going through the other paperwork that leads to that end product.
It’s like sending a letter to a friend by putting it directly in his mailbox, and then saying, “My friend filed a change of address with the postal service, and anything sent to his address is supposed to be redirected to his new address, but my letter is still sitting in his mailbox at his old address.” Well, yeah, because you didn’t actually mail the letter. You bypassed the mail system and merely replicated the end product (a letter in the mailbox). You will also find that if you go to the postal service’s Web site and ask for a delivery confirmation of the letter, the Web site is going to say, “Sorry, we have no record of having delivered that letter to that mailbox.”
The window manager does not have a fake message detector that looks to see if you posted a fake input message, and if so, reverse-engineers the logic that led to that fake input message and internally simulates the actions of that reverse-engineered logic. If you post a
WM_CHAR message, it’s not going to say, “Well, let me see, to get to that message, the user needed to have pressed the Shift key, then pressed the A key, then released the A key, then released the Shift key, so I’ll retroactively send out
WH_KEYBOARD hook notifications for those events, and if any of the hooks blocks the event, then I created a time paradox, and I have to go back in time and kill the program that posted the
WM_CHAR message.” (It’s also not going to insert the posted message in the processing queue in the correct order relative to true input messages.)
If you want to simulate input, you need to send it through the input system with functions like