Sign in to follow this  
NickGravelyn

[.net] Simulating Windows Events

Recommended Posts

NickGravelyn    855
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?

Share this post


Link to post
Share on other sites
NickGravelyn    855
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?

Share this post


Link to post
Share on other sites
Headkaze    607
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this