Solved

simulating mouse moves & mouse clicks

Posted on 2001-09-06
9
444 Views
Last Modified: 2010-04-06


i want to make some simple program that will simulate mouse actions same as if user move and click with the mouse.
Anyone knows how to do that?
0
Comment
Question by:zastava101
9 Comments
 
LVL 9

Expert Comment

by:ITugay
Comment Utility
Hi zastava101,

just take a look at "mouse_event" API function, there is all you need.


--------
Igor.
0
 
LVL 2

Expert Comment

by:lucika
Comment Utility
Example:

Mouse Move
mouse_event(
   MOUSEEVENTF_ABSOLUTE     or MOUSEEVENTF_MOVE,
    dx,  dy,  0,   GetMessageExtraInfo());

Mouse Left Button Down
mouse_event(
   MOUSEEVENTF_ABSOLUTE     or MOUSEEVENTF_LEFTDOWN,
    dx,  dy,  0,   GetMessageExtraInfo());

Mouse Left Button Up
mouse_event(
   MOUSEEVENTF_ABSOLUTE     or MOUSEEVENTF_LEFTUP,
    dx,  dy,  0,   GetMessageExtraInfo());

------------------------------------------------------------
The mouse_event function synthesizes mouse motion and button clicks.

VOID mouse_event(

    DWORD  dwFlags,     // flags specifying various motion/click variants
    DWORD  dx,     // horizontal mouse position or position change
    DWORD  dy,     // vertical mouse position or position change
    DWORD  cButtons,     // unused, reserved for future use, set to zero
    DWORD  dwExtraInfo      // 32 bits of application-defined information
   );    
Parameters

dwFlags

A set of flag bits that specify various aspects of mouse motion and button clicking. The bits in this parameter can be any reasonable combination of the following values:

Value     Meaning
MOUSEEVENTF_ABSOLUTE     Specifies that the dx and dy parameters contain normalized absolute coordinates. If not set, those parameters contain relative data: the change in position since the last reported position. This flag can be set, or not set, regardless of what kind of mouse or mouse-like device, if any, is connected to the system. For further information about relative mouse motion, see the following Remarks section.
MOUSEEVENTF_MOVE     Specifies that movement occurred.
MOUSEEVENTF_LEFTDOWN     Specifies that the left button changed to down.
MOUSEEVENTF_LEFTUP     Specifies that the left button changed to up.
MOUSEEVENTF_RIGHTDOWN     Specifies that the right button changed to down.
MOUSEEVENTF_RIGHTUP     Specifies that the right button changed to up.
MOUSEEVENTF_MIDDLEDOWN     Specifies that the middle button changed to down.
MOUSEEVENTF_MIDDLEUP     Specifies that the middle button changed to up.
The flag bits that specify mouse button status are set to indicate changes in status, not ongoing conditions. For example, if the left mouse button is pressed and held down, MOUSEEVENTF_LEFTDOWN is set when the left button is first pressed, but not for subsequent motions. Similarly, MOUSEEVENTF_LEFTUP is set only when the button is first released.

dx

Specifies the mouse's absolute position along the x-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is given as the mouse's actual x-coordinate; relative data is given as the number of mickeys moved.

dy

Specifies the mouse's absolute position along the y-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is given as the mouse's actual y-coordinate; relative data is given as the number of mickeys moved.

cButtons

This parameter is currently unused. It is reserved for future use. It must be set to zero.

dwExtraInfo

Specifies an additional 32-bit value associated with the mouse event. An application calls GetMessageExtraInfo to obtain this extra information.
0
 
LVL 2

Expert Comment

by:lucika
Comment Utility
Example:

Mouse Move
mouse_event(
   MOUSEEVENTF_ABSOLUTE     or MOUSEEVENTF_MOVE,
    dx,  dy,  0,   GetMessageExtraInfo());

Mouse Left Button Down
mouse_event(
   MOUSEEVENTF_ABSOLUTE     or MOUSEEVENTF_LEFTDOWN,
    dx,  dy,  0,   GetMessageExtraInfo());

Mouse Left Button Up
mouse_event(
   MOUSEEVENTF_ABSOLUTE     or MOUSEEVENTF_LEFTUP,
    dx,  dy,  0,   GetMessageExtraInfo());

------------------------------------------------------------
The mouse_event function synthesizes mouse motion and button clicks.

VOID mouse_event(

    DWORD  dwFlags,     // flags specifying various motion/click variants
    DWORD  dx,     // horizontal mouse position or position change
    DWORD  dy,     // vertical mouse position or position change
    DWORD  cButtons,     // unused, reserved for future use, set to zero
    DWORD  dwExtraInfo      // 32 bits of application-defined information
   );    
Parameters

dwFlags

A set of flag bits that specify various aspects of mouse motion and button clicking. The bits in this parameter can be any reasonable combination of the following values:

Value     Meaning
MOUSEEVENTF_ABSOLUTE     Specifies that the dx and dy parameters contain normalized absolute coordinates. If not set, those parameters contain relative data: the change in position since the last reported position. This flag can be set, or not set, regardless of what kind of mouse or mouse-like device, if any, is connected to the system. For further information about relative mouse motion, see the following Remarks section.
MOUSEEVENTF_MOVE     Specifies that movement occurred.
MOUSEEVENTF_LEFTDOWN     Specifies that the left button changed to down.
MOUSEEVENTF_LEFTUP     Specifies that the left button changed to up.
MOUSEEVENTF_RIGHTDOWN     Specifies that the right button changed to down.
MOUSEEVENTF_RIGHTUP     Specifies that the right button changed to up.
MOUSEEVENTF_MIDDLEDOWN     Specifies that the middle button changed to down.
MOUSEEVENTF_MIDDLEUP     Specifies that the middle button changed to up.
The flag bits that specify mouse button status are set to indicate changes in status, not ongoing conditions. For example, if the left mouse button is pressed and held down, MOUSEEVENTF_LEFTDOWN is set when the left button is first pressed, but not for subsequent motions. Similarly, MOUSEEVENTF_LEFTUP is set only when the button is first released.

dx

Specifies the mouse's absolute position along the x-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is given as the mouse's actual x-coordinate; relative data is given as the number of mickeys moved.

dy

Specifies the mouse's absolute position along the y-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is given as the mouse's actual y-coordinate; relative data is given as the number of mickeys moved.

cButtons

This parameter is currently unused. It is reserved for future use. It must be set to zero.

dwExtraInfo

Specifies an additional 32-bit value associated with the mouse event. An application calls GetMessageExtraInfo to obtain this extra information.
0
 

Author Comment

by:zastava101
Comment Utility
Can you pls post some simple code implementing these APIs from your answer? only to point me out how to start and use these api functions?

thanks in advanced.
0
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

 
LVL 2

Expert Comment

by:lucika
Comment Utility
{For Example Click Mouse Left Button on Button1 and See}
{Do not moving mouse}
procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
  dx,dy: integer;
begin
  dx:=0; dy:=0;
  {Moving Mouse}
  for i:=0 to 400 do
  begin
    case i div 100 of
    0:
    begin
      dx:=0; dy:=1;
    end;
    1:
    begin
      dx:=1; dy:=0;
    end;
    2:
    begin
      dx:=-1; dy:=0;
    end;
    3:
    begin
      dx:=0; dy:=-1;
    end;

    end;
    mouse_event(
     MOUSEEVENTF_MOVE,
     dx, dy,  0,   GetMessageExtraInfo());
     sleep(10);
  end;

  {Mouse Left Button Click}
  mouse_event(
    MOUSEEVENTF_LEFTDOWN,
    dx, dy,  0,   GetMessageExtraInfo());
  mouse_event(
    MOUSEEVENTF_LEFTUP,
    dx, dy,  0,   GetMessageExtraInfo());

end;
0
 
LVL 44

Expert Comment

by:CrazyOne
Comment Utility
Umm lucika it is not necessary to keep proposing answers. If the questioner finds a comment you posted to be the one that works they can accept that comment as the answer. By proposing answers it tends to disrupt the flow of the question sometimes. You usually don't see to many of the folks that have been here for awhile proposing answers much any more. Most of the time it is new Experts that  do this because they have not been around long enough to understand the protocol. Most of the time most of us try to approach this as team work and not be overly concerned about who gets the points. :>)
0
 
LVL 2

Accepted Solution

by:
DidierD earned 90 total points
Comment Utility
Hi,

i found these examples at http://delphi.about.com/sitesearch.htm?terms=mouse&SUName=delphi&type=0&TopNode=3042 there are some more examples.

example 1

procedure SetMousePos(x, y: longint);
var pt: TPoint;
begin
  pt := ClientToScreen(point(x, y));
  SetCursorPos(pt.x, pt.y);
end;


example 2

//simulating mouse move
procedure TForm1.Button1Click(Sender: TObject);
var pt : TPoint;
begin
  Application.ProcessMessages;
  Screen.Cursor := crHourglass;
  GetCursorPos(pt);
  SetCursorPos(pt.x + 1, pt.y + 1);
  Application.ProcessMessages;
  SetCursorPos(pt.x - 1, pt.y - 1);
  Screen.Cursor := crArrow
end;


example 3

//simulating mouse click
//we need 2 buttons on the form
procedure TForm1.Button1Click(Sender: TObject);
var
  Pt : TPoint;
begin
  Application.ProcessMessages;
 {Get the point in the center of Button 2}
  Pt.x := Button2.Left + (Button2.Width div 2);
  Pt.y := Button2.Top + (Button2.Height div 2);
 {Convert Pt to screen coordinates and Mickeys}
  Pt := ClientToScreen(Pt);
  Pt.x := Round(Pt.x * (65535 / Screen.Width));
  Pt.y := Round(Pt.y * (65535 / Screen.Height));
 {Simulate the mouse move}
  Mouse_Event(MOUSEEVENTF_ABSOLUTE or
              MOUSEEVENTF_MOVE,
              Pt.x, Pt.y, 0, 0);
 {Simulate the left mouse button down}
  Mouse_Event(MOUSEEVENTF_ABSOLUTE or
              MOUSEEVENTF_LEFTDOWN,
              Pt.x, Pt.y, 0, 0);;
 {Simulate the left mouse button up}
  Mouse_Event(MOUSEEVENTF_ABSOLUTE or
              MOUSEEVENTF_LEFTUP,
              Pt.x, Pt.y, 0, 0);;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage('Button 2 clicked')
end;


example 4

 //restrict the mouse mouvement to form and release
 //this restriction after a click on the form
procedure TForm1.FormCreate(Sender: TObject);
var r : TRect;
begin
   //it would be good idea to move the
   //mouse inside the form before restriction
  r := BoundsRect;
  ClipCursor(@R);
end;

procedure TForm1.FormClick(Sender: TObject);
begin
  //always be sure to release the cursor
  ClipCursor(nil);
end;

 
Didier
0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, please comment to advise the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Please click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20001252.html
http://www.experts-exchange.com/questions/Q.20026086.html
http://www.experts-exchange.com/questions/Q.20066091.html
http://www.experts-exchange.com/questions/Q.20070791.html
http://www.experts-exchange.com/questions/Q.20070795.html
http://www.experts-exchange.com/questions/Q.20081086.html
http://www.experts-exchange.com/questions/Q.20081708.html
http://www.experts-exchange.com/questions/Q.20082186.html
http://www.experts-exchange.com/questions/Q.20087813.html
http://www.experts-exchange.com/questions/Q.20094507.html
http://www.experts-exchange.com/questions/Q.20103740.html
http://www.experts-exchange.com/questions/Q.20104227.html
http://www.experts-exchange.com/questions/Q.20108539.html
http://www.experts-exchange.com/questions/Q.20112131.html
http://www.experts-exchange.com/questions/Q.20118798.html
http://www.experts-exchange.com/questions/Q.20141336.html
http://www.experts-exchange.com/questions/Q.20157666.html
http://www.experts-exchange.com/questions/Q.20159442.html
http://www.experts-exchange.com/questions/Q.20177987.html
http://www.experts-exchange.com/questions/Q.20182504.html
http://www.experts-exchange.com/questions/Q.20193634.html
http://www.experts-exchange.com/questions/Q.20194167.html
http://www.experts-exchange.com/questions/Q.20090036.html
http://www.experts-exchange.com/questions/Q.20265702.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20002824.html
http://www.experts-exchange.com/questions/Q.20179302.html
http://www.experts-exchange.com/questions/Q.20182632.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
Unresponsive to requests, finalized today.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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