Moving a mouse pointer with code

I see all of the mouse events which respond to moving a mouse or clicking a button.  Is there a way to cause the mouse pointer to move using code?

Thank you
mayfieldjrAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
One way...move the mouse and left click at a specified position:
(these are ABSOLUTE screen coordinates in PIXELS)

Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, _
    ByVal y As Long) As Long

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, _
    ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, _
    ByVal dwExtraInfo As Long)

Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4

Public Sub MouseClick(x As Long, y As Long)
    SetCursorPos x, y
    mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
    mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub
0
 
MufloneCommented:
surely you can, using the SetCursorPos API:

Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long

and where you need it:
Call SetCursorPos(XPos, YPos)
having XPos and YPos the coordinates to move in pixels

regards
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.