同时支持Directx input 和 Windows message的键盘模拟方法
2013-06-09 02:51
225 查看
[转]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
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
相关文章推荐
- 让Web API同时支持多个Get方法
- IE6和Firefox同时支持PNG透明图片的方法
- VB模拟键盘输入的N种方法
- win2012中让IIS同时支持多版本ASP.NET 3.5/4.0/4.5的方法
- iOS UITextField禁止键盘弹出,同时又实现代理方法的实现方式!
- 用VB模拟键盘事件的N种方法
- keyPressEvent,QWS_KEYBOARD,QWS_MOUSE_PROTO,嵌入式qt同时支持键盘和触摸屏
- 给Jquery添加alert,prompt方法,类似系统的Alert,Prompt,可以响应键盘,支持拖动
- 让Thinkpad USB键盘支持小红帽中键滚轮的方法(ThinkPad USB Keyboard with TrackPoint)
- 优化IIS7.5支持10万个同时请求的配置方法
- Apache同时支持PHP和Python的配置方法
- 童鞋们,我模拟了Google的电吉他,可录音,支持键盘
- win7设定固定ip和同时支持dhcp的方法
- MOSS站点同时支持Windows集成验证与Form验证的配置方法(一)
- MOSS站点同时支持Windows集成验证与Form验证的配置方法(四)
- 优化IIS7.5支持10万个同时请求的配置方法_win服务器
- 让IE ff Opera同时支持Alpha透明的方法
- 让IE ff Opera同时支持Alpha透明的方法
- VB模拟键盘事件的N种方法