Solved

Windows Simulate Mouse Left Click

Posted on 2006-11-28
1
1,395 Views
Last Modified: 2008-01-09
Hi , how do I simulate a mouse left click (in windows) ?

ie. when i run this code , it is exactly as if the user has clicked the left mouse button ?

(please don't send me an msdn page)

Thank you,

Mark
0
Comment
Question by:Paladin_VB
1 Comment
 
LVL 6

Accepted Solution

by:
SeanDurkin earned 70 total points
ID: 18030467
If you want to simulate a mouse-click in Windows, I recommend using the mouse_event() function, specifying the MOUSEEVENTF_ABSOLUTE and MOUSEEVENTF_LEFTDOWN flags. However, I think you need to move the cursor to that position before you simulate the click, so you should throw in a SetCursorPos() function, just to be safe:

int coordX, coordY;

// sets the mouse's position
SetCursorPos(coordX, coordY);

// simulates the click
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, (DWORD) coordX, (DWORD) coordY, 0, 0);
// use a pause, if you wish
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP,      (DWORD) coordX, (DWORD) coordY, 0, 0);


While I realize you said don't print MSDN pages, there are parameters of the mouse_event() function that I didn't use, and you may wish to, so here are the pages to the functions I used:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/mouseinput/mouseinputreference/mouseinputfunctions/mouse_event.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/cursors/cursorreference/cursorfunctions/setcursorpos.asp

This should do the trick, although some may also recommend the SendInput() function, but trust me, the mouse_event() function works fine for me.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now