The only correct way to programmatically simulate keyboard or mouse input is using the API
SendInput
:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[
^].
All other methods, such as sending a message, using some library APIs, depend on many factors, such as having a window prepared to handle messages in certain way, position of keyboard focus, and things like that, so such kinds of simulation won't work in general case, even when they can be used for those special cases. In contrast to that
SendInput
acts as it was called triggered by real event through the real device driver.
—SA