Create an On-screen Keyboard

You must compromise:

If you want to simulate keyboard input, then you must use SendInput, which means being at the mercy of which window currently has focus. SendInput is like hitting the keys on your physical keyboard. The only way you can send your keystrokes to a specific window using your keyboard is to ALT+TAB to the right window.

If you want to send keystrokes to a specific window, then you incur funky behavior:

  1. Applications handle input differently. And simple WM_KEYDOWN / WM_KEYUP messages are not the only way to detect keyboard input. For example there is also the keyboard state (GetKeyboardState()) which you will have a harder time simulating. This is most likely what you’re experiencing.
  2. Applications may RELY on the standard behavior of having focus while receiving keyboard input messages. By posting messages to these applications, you invoke strange out-of-order behavior that may crash them.
  3. Now multiple windows on the system can be receiving keyboard input at the same time. This might also cause strange behavior.
  4. (etc…) Hooks won’t be called for this input, your keyboard / input drivers won’t see it, it won’t be recognized by things like DirectInput… basically it’s a never-ending patchwork of issues by doing something the bad-bear way.

There is no way around those side-effects; it’s the consequence of doing shady stuff.


A solution for your purposes, because you’re targeting a single specific application, may be to use PostMessage in conjunction with SetKeyboardState to simulate the keyboard state including shift positions.

Leave a Comment