Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Left Mouse Click (http://www.programmingforums.org/showthread.php?t=15427)

crawforddavid2006 Mar 16th, 2008 1:02 PM

Left Mouse Click
 
Sorry if this is a stupid question, but is there a simple way to make my program simulate a left mouse click? if so, could someone please point me in the right direction because everything i can find is complicated, and doesnt seem to work. also i am using .net 2.0

lectricpharaoh Mar 16th, 2008 9:18 PM

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:
:

  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]

crawforddavid2006 Mar 16th, 2008 10:42 PM

Re: Left Mouse Click
 
That worked perfectly, thank you so much.

lectricpharaoh Mar 16th, 2008 11:20 PM

Re: Left Mouse Click
 
Glad it worked out for you. Now you can also simulate right clicks, middle clicks, double clicks, and all the rest. :)

crawforddavid2006 Mar 17th, 2008 3:06 PM

Re: Left Mouse Click
 
now if i'm simulating key strokes, what do i need to change?

Ooble Mar 18th, 2008 3:01 AM

Re: Left Mouse Click
 
I believe you're looking for SendKeys or SendInput. Unfortunately, MSDN is down for me at the mo so I can't check.

crawforddavid2006 Mar 18th, 2008 9:23 PM

Re: Left Mouse Click
 
but what about all that dx, dy, and dwExtraInfo stuff. what does that need to be changed to if anything?

crawforddavid2006 Mar 19th, 2008 8:53 PM

Re: Left Mouse Click
 
i tried
:

SendKeys.Send("{ENTER}");

but nothing happens. and no matter what keys i send, nothing happens.

lectricpharaoh Mar 19th, 2008 11:46 PM

Re: Left Mouse Click
 
While you can use SendInput(), as I mentioned in my initial post and Ooble mentioned above, it's harder to use than the alternative: keybd_event(), used much like mouse_event() from my code above. You'll need to use p/invoke, just like you did for mouse_event(), since it's a native API routine for which there is no managed version. You'll also need a DllImport directive so it can marshal parameters:
:

  1. [DllImport("user32.dll")] static extern uint keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

The first parameter is the virtual key code (list here). The second is the hardware scan code; there is a list of these on the page about keybd_event() that you can use if you like. Alternatively, you can pass zero instead, provided you pass a valid virtual key code as the first parameter, which is a more portable way of doing things. The third contains flags that specify if it's an extended key (you only need to worry about this if dealing with hardware scan codes, but not with virtual key codes) and/or a key release event; if the latter is not set, it's a key press event. The final parameter can just be zero as well; I expect it's for custom input devices that send additional data. To my knowledge, standard keyboards don't use this field at all.

If you look at the code I posted for mouse_input(), this will end up being quite similar (actually, it will be a tad simpler). If you have further problems, let me know, but right now I'm off to play D2. :)

crawforddavid2006 Mar 21st, 2008 5:54 PM

Re: Left Mouse Click
 
no matter what i do, it keeps giving me this error

:

Exception System.TypeLoadException was thrown in debuggee:
Could not load type 'MouseAndKeyboard.Mouse' from assembly 'MouseAndKeyboard, Version=1.0.3002.32195, Culture=neutral, PublicKeyToken=null' because the method 'keybd_event' has no implementation (no RVA).买礤m

OnClick()
OnClick()
OnMouseUp()
WmMouseUp()
WndProc()



All times are GMT -5. The time now is 3:42 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC