同時支援Directx input 和 Windows message的鍵盤類比方法

來源:互聯網
上載者:User

[轉]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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.