Sending keyboard events to another application in C# that does not handle Windows events

白昼怎懂夜的黑 提交于 2021-02-06 10:16:05

问题


here is my situation: we are writing an application that must transform Microsoft Kinect coordinates into keyboard and mouse events.

When we need to take control of the mouse, everything works as we intended in ANY kind of application. The problem arises when we need to send keyboard events (like key down or key up) to applications that doesn't handle Windows events, like games, for example.

We tried the SendKeys class of the .net framework, and it only works with Windows applications. When the application is a game like Half-Life or Doom we can't get the same effect. So, here is my question: how can we effectively send keyboard events to these other applications?


回答1:


You need to simulate input using SendInput. SendMessage and SendKeys tend to work at the level of windows messages - but DirectX apps don't run a traditional message loop.

There is a page on PInvoke.NET, but I have to confess, I've not tried to use it.


See also this thread on the GameDev.net site, where someone has worked with some of the "teething" issues that can be encountered specifically interacting with a DirectInput based application.




回答2:


Unfortunately DirectX has no built-in hooking functionality; however there are some (this one is a .Net wrapper over another one) libraries out there that can do it. You will also need to look into XInput, which deprecates DirectInput.




回答3:


I think following post should be an answer to your question,

How do i send keys using a global keyboard hook?

According to the answer, you have to use following key codes to the SendInput function, http://www.gamespp.com/directx/directInputKeyboardScanCodes.html




回答4:


You need to pInvoke Winapi sendmessage.

http://msdn.microsoft.com/en-us/library/ms644950(v=vs.85).aspx

http://pinvoke.net/default.aspx/user32.SendMessage

This is what I'm using. It works but you have to read a little bit about it. This is very powerful method.




回答5:


This is a quick and easy solution that I'm using. Works well.

InputSimulator



来源:https://stackoverflow.com/questions/7136000/sending-keyboard-events-to-another-application-in-c-sharp-that-does-not-handle-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!