How do i send keys using a global keyboard hook?

2020-07-18 08:50发布

问题:

I'm trying to send keys to an application which does not respond to any of the API's that I have so far used (SendInput(), PostMessage(), SendMessage() and others). However, i tested Windows' On-Screen Keyboard Utility and pressed the keys i needed and the application received these keys easily.

If i understand correctly the keyboard utility uses global keyboard hooks to send the keys, so I'm interested i how i could do so as well. I have tried finding examples of how this can be done on google but have come up with no results.

Thanks.

回答1:

Hooks are used to intercept the events, not to send new event. So what you're asking is impossible with hooks. SendInput is the way to go. probably you did something wrong, so it'll be better if you post your code that doesn't work and debug what went wrong.



回答2:

I solved it. I was sending keys to a DirectX application. Turns out i had to send DirectX scan codes instead of regular VK_KEY's.

A list of scan codes can be found at: http://www.gamespp.com/directx/directInputKeyboardScanCodes.html

Using these with SendInput() and everything worked great.

Thanks for the help also.

Cheers.