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

5

12

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?

Wessels answered 21/8, 2011 at 2:49 Comment(3)
I don't know, I'm a C# noob :)Wessels
Yeah DirectInput would be the reason the apps aren't 'normal'.Parik
sounds like a nice project there!Penstemon
C
3

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.

Crow answered 23/8, 2011 at 13:25 Comment(2)
Already tried that, didn't work. :( That's why I opened a bounty on this question.Wessels
@Kico - according to the Gamedev.net thread I linked to, you have to send scancodes rather than virtual keys. Have you tried that?Crow
P
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.

Parik answered 23/8, 2011 at 13:26 Comment(1)
No, the idea of this form of hooking is that you inject your own code into the application before it has the opportunity to create the object AT RUNTIME. This new code will cause your own object to be created. That is why it has to be in C/++ (but there is a .Net wrapper there). Also remember that this type of hooking will make cheat monitors(e.g. Punk Buster) go absolutely bonkers.Parik
C
1

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

Carbonate answered 28/8, 2011 at 17:36 Comment(0)
B
0

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.

Briton answered 23/8, 2011 at 7:38 Comment(1)
SendMessage is not a good solution either :( Already tried that too and didn't work as I wanted. As InputSimulator, it didn't send input to game applications either. :(Wessels
A
0

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

InputSimulator

Ambagious answered 23/8, 2011 at 8:32 Comment(1)
Alrady tried that :( Works fine for "traditional" Windows applications, but when sending inputs to applications like "Half-Life" or "Doom" it simply doesn't work.Wessels

© 2022 - 2024 — McMap. All rights reserved.