Solved

Windows Simulate Mouse Left Click

Posted on 2006-11-28
1
1,399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

733 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