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

Comments

  1. 01 Jul 2007 at 21:06
    Took me a while to get it to work, but I have, and I just thought I'd share my problems, incase others experience the same:

    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
  2. 13 Apr 2007 at 08:19
    "** Please note that this code does not work in Windows 2000. You need to use the SendInput API instead***"

    Does this work on XP? Or it doesn't work on NT based systems in general?
  3. 06 May 2006 at 13:01

    I get an error that the user32.dll doesn't declare the command Mouse_Event
    can someone help me?


  4. 04 Jun 2003 at 17:44

    makes the mouse move...

  5. 04 Jun 2003 at 17:42

    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

  6. 01 Jan 1999 at 00:00

    This thread is for discussions of Simulating Mouse Events.

Leave a comment

Sign in or Join us (it's free).

 Dominique

Related discussion

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...

Want to stay in touch with what's going on? Follow us on twitter!