Sign in to follow this  
Moe

[.net] Generating Windows Mouse events in C#?

Recommended Posts

So I had an idea a few days ago that I thought would be kind of cool to do. I've got a wireless Xbox 360 controller and the Wireless Receiver for Windows, which works great for games. This got me thinking - why not write a small XNA app that sits down in the system tray without a main graphical window and allows the user to use the Xbox controller like a mouse - having one of the thumbsticks moving the cursor around and the A (green) button being a left click and B be a right click. I don't have any sort of remote control for my computer (which I use as my entertainment system - DVD player, etc), and I figured the controller would make a great remote. Getting and setting the cursor position based on the gampead input is easy enough in XNA. What I don't know how to do is send a mouse click event to Windows when the user presses a button on the gamepad. I do realize that there are a number of security concerns that come up with applications that send events to Windows, but is it possible to generate a mouse click event and send it to Windows using the .Net framework?

Share this post


Link to post
Share on other sites
You can use System.Windows.Forms.Cursor.Position to get the mouse position and the mouse_event() API function to move the mouse. Quite easy to do what you want actually.

http://pinvoke.net/default.aspx/user32/mouse_event.html

Share this post


Link to post
Share on other sites
Yeah, setting the cursor position isn't a problem. What the problem is, is sending windows a Click event telling that the mouse has clicked at a certain position. Along with this, is there a way to send windows keystroke messages as well?

Quote:
Original post by Headkaze
You can use System.Windows.Forms.Cursor.Position to get the mouse position and the mouse_event() API function to move the mouse. Quite easy to do what you want actually.

http://pinvoke.net/default.aspx/user32/mouse_event.html


Ah, thanks for the link!

Share this post


Link to post
Share on other sites
Quote:
Original post by Moe
Yeah, setting the cursor position isn't a problem. What the problem is, is sending windows a Click event telling that the mouse has clicked at a certain position. Along with this, is there a way to send windows keystroke messages as well?

Quote:
Original post by Headkaze
You can use System.Windows.Forms.Cursor.Position to get the mouse position and the mouse_event() API function to move the mouse. Quite easy to do what you want actually.

http://pinvoke.net/default.aspx/user32/mouse_event.html


Ah, thanks for the link!


Oops yeah I actually meant mouse_event() to press left or right buttons not "move the mouse". And yes Cursor.Position is not read only so you can use that to move the cursor :)

Check out keybd_event() API for sending keystrokes.

http://pinvoke.net/default.aspx/user32/keybd_event.html

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