views:

1450

answers:

2
+10  A: 

Simulate keyboard input using SendInput, not PostMessage.

You can't simulate keyboard input with PostMessage.

There are still some caveats with respect to keyboard state/async-state:

The SendInput function does not reset the keyboard's current state. Therefore, if the user has any keys pressed when you call this function, they might interfere with the events that this function generates. If you are concerned about possible interference, check the keyboard's state with the GetAsyncKeyState function and correct as necessary.


The lParam for the WM_KEYDOWN Notification is specified in terms of the bits of the field:

  • The first 16 bits are the repeat count
  • The next 8 bits are the scan code
  • The next bit is 1 for extended key, 0 otherwise
  • The next 4 bits are reserved and must be 0
  • The next bit is always 0 (for WM_KEYDOWN)
  • The next bit is the previous key state
  • The last bit is always 0 (for WM_KEYDOWN)

A warning: Any solution you build based around PostMessage is going to be very brittle.

Kevin Montrose
Well I'd like to use this as it'd allow me to send input without having focus on the window. Is there any other way to accomplish this?
devoured elysium
Not that I am aware of.
Kevin Montrose
A: 

The message thread is way too much in past but just in case someone has a similar question ...

In Spy++ if you right click on the highlighted (logged message) entry and look at its properties, You can see the exact value of the lParam. You can then use that as your lParam to ensure that the PostMessage leads to similar effects, as the manual action did.

-- cheers

pdhoolia