Library code snippets
Simulating Mouse Events
A neat little module letting you simulate
the user moving the mouse, and clicking on objects. Particularly useful for
tutorial-style help. To move the mouse, call SetCursorPos(x,y), where x and
y are the xy co-ordinates on the screen (in pixels). Then call the LeftDown,
LeftClick etc procedures to simulate the mouse clicking.
** Please note that this code does not work in Windows 2000. You need to use
the SendInput API instead***
Option Explicit
Public 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)
Public Declare Function SetCursorPos Lib "user32" (ByVal X As Long,
ByVal Y As Long) As Long
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI)
As Long
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20
Public Const MOUSEEVENTF_MIDDLEUP = &H40
Public Const MOUSEEVENTF_RIGHTDOWN = &H8
Public Const MOUSEEVENTF_RIGHTUP = &H10
Public Const MOUSEEVENTF_MOVE = &H1
Public Type POINTAPI
X As Long
Y As Long
End Type
Public Function GetCurrentX() As Long
Dim Position As POINTAPI
GetCursorPos Position
GetCurrentX = Position.X
End Function
Public Function GetCurrentY() As Long
Dim Position As POINTAPI
GetCursorPos Position
GetCurrentY = Position.Y
End Function
Public Sub LeftClick()
LeftDown
LeftUp
End Sub
Public Sub LeftDown()
Mouse_Event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
End Sub
Public Sub LeftUp()
Mouse_Event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub
Public Sub MiddleClick()
MiddleDown
MiddleUp
End Sub
Public Sub MiddleDown()
Mouse_Event MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0
End Sub
Public Sub MiddleUp()
Mouse_Event MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0
End Sub
Public Sub MoveMouse(xMove As Long, yMove As Long)
Mouse_Event MOUSEEVENTF_MOVE, xMove, yMove, 0, 0
End Sub
Public Sub RightClick()
RightDown
RightUp
End Sub
Public Sub RightDown()
Mouse_Event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0
End Sub
Public Sub RightUp()
Mouse_Event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
End Sub
Related articles
Related discussion
-
Bluetooth Mouse And Keyboard Combo - Wireless Bluetooth Scanner Reader
by JacobS (0 replies)
-
Bluetooth Keyboard Mouse | Bluetooth Acpi Device Driver
by JacobS (0 replies)
-
Bluetooth Keyboard And Mouse, Bluetooth Gps For Pdas
by JacobS (0 replies)
-
Bluetooth Keyboard And Mouse Combo. Bluetooth Razor
by JeremyS (0 replies)
-
Bluetooth Mouse 5000, Copilot Bluetooth Gps
by JamesO (0 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
It was a while since I did it, and I can't find my source code, but basicaly the problem was with the datatypes, I changed all occurences of long to Integer (i think).
Hope this might help anyone with any problems, though I know this won't affect many people.
And thanks to the author for the code :)
Excellent
Does this work on XP? Or it doesn't work on NT based systems in general?
I get an error that the user32.dll doesn't declare the command Mouse_Event
can someone help me?
makes the mouse move...
I am kinda new at this stuff, but when I try to call that sub it tells me that i need a = after the (#,#) part
Am I missing something?
-Thanks
This thread is for discussions of Simulating Mouse Events.