Solved

Windows Simulate Mouse Left Click

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

705 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

18 Experts available now in Live!

Get 1:1 Help Now