![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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
__________________
|
|
|
|
|
|
#2 |
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 927
Rep Power: 4
![]() |
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)
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]
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp Last edited by lectricpharaoh; Mar 16th, 2008 at 9:32 PM. |
|
|
|
|
|
#3 |
|
Expert Programmer
|
Re: Left Mouse Click
That worked perfectly, thank you so much.
__________________
|
|
|
|
|
|
#4 |
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 927
Rep Power: 4
![]() |
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.
![]()
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp |
|
|
|
|
|
#5 |
|
Expert Programmer
|
Re: Left Mouse Click
now if i'm simulating key strokes, what do i need to change?
__________________
|
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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. |
|
|
|
|
|
#7 |
|
Expert Programmer
|
Re: Left Mouse Click
but what about all that dx, dy, and dwExtraInfo stuff. what does that need to be changed to if anything?
__________________
|
|
|
|
|
|
#8 |
|
Expert Programmer
|
Re: Left Mouse Click
i tried
SendKeys.Send("{ENTER}");but nothing happens. and no matter what keys i send, nothing happens.
__________________
|
|
|
|
|
|
#9 |
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 927
Rep Power: 4
![]() |
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)
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. ![]()
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp |
|
|
|
|
|
#10 |
|
Expert Programmer
|
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()
__________________
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |