Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 16th, 2008, 1:02 PM   #1
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 579
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
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
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
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,005
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
Old Mar 16th, 2008, 10:42 PM   #3
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 579
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Re: Left Mouse Click

That worked perfectly, thank you so much.
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Mar 16th, 2008, 11:20 PM   #4
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,005
Rep Power: 5 lectricpharaoh will become famous soon enough
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.
__________________
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
lectricpharaoh is offline   Reply With Quote
Old Mar 17th, 2008, 3:06 PM   #5
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 579
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Re: Left Mouse Click

now if i'm simulating key strokes, what do i need to change?
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Mar 18th, 2008, 3:01 AM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 18th, 2008, 9:23 PM   #7
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 579
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Re: Left Mouse Click

but what about all that dx, dy, and dwExtraInfo stuff. what does that need to be changed to if anything?
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Mar 19th, 2008, 8:53 PM   #8
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 579
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Re: Left Mouse Click

i tried
SendKeys.Send("{ENTER}");

but nothing happens. and no matter what keys i send, nothing happens.
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Mar 19th, 2008, 11:46 PM   #9
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,005
Rep Power: 5 lectricpharaoh will become famous soon enough
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:
C# Syntax (Toggle Plain Text)
  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.
__________________
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
lectricpharaoh is offline   Reply With Quote
Old Mar 21st, 2008, 5:54 PM   #10
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 579
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
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()
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementation of Mouse Events. smita Existing Project Development 3 Mar 15th, 2007 3:11 PM
Java applet mouse event. Some trouble... glopal Java 5 Mar 9th, 2007 3:59 PM
Hyperactive mouse Polyphemus_ Coder's Corner Lounge 4 Jan 13th, 2007 10:59 PM
Mouse problems coldDeath Coder's Corner Lounge 8 Jul 5th, 2006 5:11 PM
Help: Changing mouse input to keyboard? Alighieri C++ 2 Mar 17th, 2005 9:59 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:13 PM.

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