How to send keyboard events to all kind of applications in windows?

We have a simple C++ program which send/simulate return key events to an active window using keybd_event.

keybd_event(VK_RETURN, 0x1c, 0, 0);

This works fine for all kind of windows apps. But we like to send the keyboard event to a 3rd party app like GTA (the game) - and this does not work. GTA does not receive the keyboard event.

Why does GTA not receive the keyboard event? Is there a way to get it working with keybd_event? Or is there any other solution to send events to all kind of apps (with a common windows or C++ standard library)?


keybd_event is the obsolete API call. Try to use moder SendInput API function

UINT WINAPI SendInput(
  _In_ UINT    nInputs,
  _In_ LPINPUT pInputs,
  _In_ int     cbSize
);

nInputs The number of structures in the pInputs array.

pInputs An array of INPUT structures. Each structure represents an event to be inserted into the keyboard or mouse input stream.

cbSize The size, in bytes, of an INPUT structure. If cbSize is not the size of an INPUT structure, the function fails.

This function is subject to UIPI. Applications are permitted to inject input only into applications that are at an equal or lesser integrity level.

The SendInput function inserts the events in the INPUT structures serially into the keyboard or mouse input stream. These events are not interspersed with other keyboard or mouse input events inserted either by the user (with the keyboard or mouse) or by calls to keybd_event , mouse_event , or other calls to SendInput .

This function does not reset the keyboard's current state. Any keys that are already pressed when the function is called might interfere with the events that this function generates. To avoid this problem, check the keyboard's state with the GetAsyncKeyState function and correct as necessary.

Because the touch keyboard uses the surrogate macros defined in winnls.h to send input to the system, a listener on the keyboard event hook must decode input originating from the touch keyboard. For more information, see Surrogates and Supplementary Characters.

An accessibility application can use SendInput to inject keystrokes corresponding to application launch shortcut keys that are handled by the shell. This functionality is not guaranteed to work for other types of applications.


This API is low-level; so applications do not respond to it directly. The function is called by interrupt handler. Maybe you are generating not exactly same input which you think you need for this application to respond. Also, this API is currently superseded by the function SendInput which I recommend to use instead of keybd_event.

链接地址: http://www.djcxy.com/p/18442.html

上一篇: Ruby on Rails服务器选项

下一篇: 如何将键盘事件发送到Windows中的所有类型的应用程序?