Library tutorials & articles
Simulating User Actions
- Mouse Movements
- Keyboard Events
- Buttons being clicked
Mouse Movements
Here is 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 ***
Many thanks to Dominique for sending
me this. (Originally published in our discussion
list).
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 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...
Will this code prevent the screensaver from beeing activated (if I just use the mouse movement now and then)?
/Max
I can't make it work.
I'm using this code:
Select Case tMover.Tag
Case 0
MoveMouse GetCurrentX + 10, GetCurrentY
tMover.Tag = 1
Case 1
MoveMouse GetCurrentX + 10, GetCurrentY + 10
tMover.Tag = 2
Case 2
MoveMouse GetCurrentX, GetCurrentY + 10
tMover.Tag = 3
Case 3
MoveMouse GetCurrentX - 10, GetCurrentY + 10
tMover.Tag = 4
Case 4
MoveMouse GetCurrentX - 10, GetCurrentY
tMover.Tag = 5
Case 5
MoveMouse GetCurrentX - 10, GetCurrentY - 10
tMover.Tag = 6
Case 6
MoveMouse GetCurrentX, GetCurrentY - 10
tMover.Tag = 7
Case 7
MoveMouse GetCurrentX + 10, GetCurrentY - 10
tMover.Tag = 0
End Select
End Sub
To move the mouse around in a circle with a timer. But all it do is moving the mouse towards the bottom right corner. What am I doing wrong?
I can't make it work.
I'm using this code:
Select Case tMover.Tag
Case 0
MoveMouse GetCurrentX + 10, GetCurrentY
tMover.Tag = 1
Case 1
MoveMouse GetCurrentX + 10, GetCurrentY + 10
tMover.Tag = 2
Case 2
MoveMouse GetCurrentX, GetCurrentY + 10
tMover.Tag = 3
Case 3
MoveMouse GetCurrentX - 10, GetCurrentY + 10
tMover.Tag = 4
Case 4
MoveMouse GetCurrentX - 10, GetCurrentY
tMover.Tag = 5
Case 5
MoveMouse GetCurrentX - 10, GetCurrentY - 10
tMover.Tag = 6
Case 6
MoveMouse GetCurrentX, GetCurrentY - 10
tMover.Tag = 7
Case 7
MoveMouse GetCurrentX + 10, GetCurrentY - 10
tMover.Tag = 0
End Select
End Sub
To move the mouse around in a circle with a timer. But all it do is moving the mouse towards the bottom right corner. What am I doing wrong?
[edit] Nevermind. I thought that positions were absolute. They're relative
Many many thanks for this.
I had found something somewhere else on calling API procedures but couldn't get it to work.
I had been searching for ages for something like this and am very grateful for it.
I was trying to create something in Excel that does a screen scrape of a payments system developed in-house, but one of the pages that contained the info I needed called for a right mouse click with no shortcut-key. Another thing required a mouse-click into a field that had neither an accelerator nor was it a tab-stop. So emulating a mouse move and click was the only thing I could do.
Oh the possibilities!
I wan to to do this but with a captionless button on crystal reports preview windows to force click event search button
This thread is for discussions of Simulating User Actions.