How do I emulate mouse clicks using code ? I want the mouse to actually click on something, and not JUST trigger an event.
What I am trying to do:
I am trying to set a timer to click the mouse on an X/Y Pos.
Printable View
How do I emulate mouse clicks using code ? I want the mouse to actually click on something, and not JUST trigger an event.
What I am trying to do:
I am trying to set a timer to click the mouse on an X/Y Pos.
Code:'Before you start this program, I suggest you save everything that wasn't saved yet.
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)
Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10
Private Sub Form_Activate()
Do
'Simulate a mouseclick on the cursor's position
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0&, 0&, cButt, dwEI
DoEvents
Loop
End Sub
Thanks. Works perfect. Check out the program
I built on download.com or my web site. It is
called BZAutoSurf.exe - Its cool, I promise.
Try this:
Code:Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const MK_LBUTTON = &H1
Private Sub Form_Load()
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
Dim PT As POINTAPI
Dim WND As Long
GetCursorPos PT
WND = WindowFromPoint(PT.x, PT.y)
PostMessage WND, WM_LBUTTONDOWN, MK_LBUTTON, 0
PostMessage WND, WM_LBUTTONUP, MK_LBUTTON, 0
End Sub