Solved

Windows Simulate Mouse Left Click

Posted on 2006-11-28
1
1,407 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

695 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