同时支持Directx input 和 Windows message的键盘模拟方法

[转]http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/29cf2de9-412e-44dd-9050-174089d8e2a2/

Simulating keyboard events:

SendInput() is the function to use if DirectX applications need to receive the simulated keyboard events.  This function injects keyboard instructions at driver level - so these events will be received by both DirectX applications and those based on the window message queuing system.

SendInput() supersedes keybd_event().  It's better to use SendInput() than keybd_event() because the former allows you to define an array of keyboard events (actually, a mixture of keyboard/mouse/other hardware inputs is possible) and then inject them all in the desired sequence with the guarantee that they won't be interspersed by either real hardware events or other simulated events.

My application is a keyboard remapping application for games, so I had to be able to simulate keyboard events for DirectX games like most first-person shooters, as well as games that use the window queuing system such as Age of Empires.

Intercepting and blocking events:

Hooking allows keyboard messages going to windows to be intercepted and then modified or suppressed as desired.

Using a keyboard hook, key presses can be detected system-wide regardless of whether the receiving application is DirectX-based or using message queuing.

Does anyone know how if it's possible to suppress keyboard events being detected by a DirectX application?

Update: note - DirectX applications may retrieve keyboard instructions before a Windows hook can receive notification, making them impossible to detect and then suppress.

The following example simulates CTRL-C, which I used to obtain text from a control in another application (which was a custom listbox and didn't respond to the LB_GETTEXT message) by copying to the clipboard.

 

 

Code Block

INPUT inp[4];

ZeroMemory(inp, sizeof(inp));

 

//press the CONTROL key

inp[0].type = INPUT_KEYBOARD;

inp[0].ki.wVk = VK_CONTROL;

 

//press the C key

inp[1].type = INPUT_KEYBOARD;

inp[1].ki.wVk = 'C';

 

//release the C key

inp[2] = inp[1];

inp[2].ki.dwFlags |= KEYEVENTF_KEYUP;

//release the CONTROL key

inp[3] = inp[0];

inp[3].ki.dwFlags |= KEYEVENTF_KEYUP;

//simulate keyboard events and check success of function

if (SendInput(4, inp, sizeof(INPUT)) != 4)

//...then SendInput did not correctly insert 4 keyboard instructions

//otherwise function was successful

原文地址:https://www.cnblogs.com/MaxWoods/p/3127934.html