View Single Post
Old Mar 16th, 2008, 9:18 PM   #2
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,031
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: Left Mouse Click

You can use SendInput() or mouse_event() from the Win32 API (you'll need to use p/invoke to call these from .NET). The latter has been replaced by the former, but it's a simpler function. If you choose to use SendInput() instead, it can handle both mouse and keyboard events, but you'll need to simulate unions in C#. You can do this with the various structure member alignment and layout directives, but since mouse_event() is simpler to use, I'll give an example with that:
c# Syntax (Toggle Plain Text)
  1. using System.Runtime.InteropServices;
  2. using System.Windows.Forms;
  3.  
  4. namespace YourNamespaceHere
  5. {
  6. public static class VirtualMouse
  7. {
  8. // import the necessary API function so .NET can
  9. // marshall parameters appropriately
  10. [DllImport("user32.dll")]
  11. static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
  12.  
  13. // constants for the mouse_input() API function
  14. private const int MOUSEEVENTF_MOVE = 0x0001;
  15. private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
  16. private const int MOUSEEVENTF_LEFTUP = 0x0004;
  17. private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
  18. private const int MOUSEEVENTF_RIGHTUP = 0x0010;
  19. private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
  20. private const int MOUSEEVENTF_MIDDLEUP = 0x0040;
  21. private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
  22.  
  23.  
  24. // simulates movement of the mouse. parameters specify changes
  25. // in relative position. positive values indicate movement
  26. // right or down
  27. public static void Move(int xDelta, int yDelta)
  28. {
  29. mouse_event(MOUSEEVENTF_MOVE, xDelta, yDelta, 0, 0);
  30. }
  31.  
  32.  
  33. // simulates movement of the mouse. parameters specify an
  34. // absolute location, with the top left corner being the
  35. // origin
  36. public static void MoveTo(int x, int y)
  37. {
  38. mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, 0);
  39. }
  40.  
  41.  
  42. // simulates a click-and-release action of the left mouse
  43. // button at its current position
  44. public static void LeftClick()
  45. {
  46. mouse_event(MOUSEEVENTF_LEFTDOWN, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
  47. mouse_event(MOUSEEVENTF_LEFTUP, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
  48. }
  49. }
  50. }
Note that mouse movement and position values are not given in pixels, but rather 'mickeys'; a mickey is the smallest increment of motion that the mouse can detect. Mouse sensitivity (ie, in the Windows control panel) just changes the number of mickeys the mouse must move in order for Windows to actually move the pointer.

If you like, you can add to this class to make it more full-featured, like support for the other buttons, support for dragging (you just have separate 'press' and 'release' methods, rather than combining them as in the 'click' method).

[edit] For the record, this functionality exists at a pretty low level. I'm fairly certain that mouse_event() is just the API function invoked by the mouse driver, so you can spoof mouse input for any software you like; it's not limited to the application with focus. [/edit]
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick

Last edited by lectricpharaoh; Mar 16th, 2008 at 9:32 PM.
lectricpharaoh is offline   Reply With Quote