[.net] Simulating Windows Events

Started by
1 comment, last by Headkaze 16 years, 11 months ago
Is it possible for a .NET application to simulate mouse and/or keyboard input by sending events into Windows as if it were an input device? I'm trying to write a program that will enable an Xbox 360 controller to simulate various configurable buttons so that I can play any game with it, but so far all I've been able to do is move the mouse cursor, though I doubt this will translate to a game. Any ideas? Is it even possible? I can see why such a thing would be impossible (be horrible to get a virus that moved around the mouse and simulated keyboard input), but is it somewhere in there?
Advertisement
Ok. I found http://msdn2.microsoft.com/en-us/library/ms171548.aspx which solves the keyboard input, but the method shown for the mouse input will only work for the current application. The keyboard method it shows will work for any application that's on top. Any ideas for the mouse?
I believe the SetCursorPos API function is what your after for the mouse. Check out the pinvoke.net site for the full definition.

Simulating key input is a little more difficult for things like games written in DirectX (and use DirectInput).

You can do a global keyboard hook using a hooking dll (probably needs to be written in C++ then referenced in your .NET application). Then you can place keydown/keyup messages into application's message queue.

You can inject keys into DirectInput using the APIHijack code on codeproject. Your dll basically sits between the DirectInput API and the target application so you can inject keys into the key buffer. Problem is you would need to write a bunch of code to support each release of DirectX if you wanted to support all games.

There is also the keybd_event() API function but I think that will only work with the currently focused application (which may or may not be what your after).

In the end your probably better off using something like AutoHotkey which can map joystick input to keypresses.

This topic is closed to new replies.

Advertisement