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

Started by
5 comments, last by Moe 16 years, 1 month ago
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?
Advertisement
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
You can also set the cursor's position using the Cursor.Position property. It's not "get" only.

Cursor.Position
Mike Popoloski | Journal | SlimDX
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!
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
Ah, thanks so much HeadKaze!
Anyway, here's a link to what I created using what I learned in this thread.

Thanks again guys!

This topic is closed to new replies.

Advertisement