Visual Basic Programming Code Examples Visual Basic > Other Code Examples Sending mouse events to the screen (inc. click events) Sending mouse events to the screen (inc. click events) Option Explicit 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) Public Enum eButtons eNothing = 1 eRightClick = 2 eDoubleRight = 4 eLeftClick = 8 eDoubleLeft = 16 End Enum 'Purpose : Sends mouse events to the screen 'Inputs : [dX] The change in the x coordinate of the mouse (if 0 cursor doesn't move) ' [dy] The change in the y coordinate of the mouse (if 0 cursor doesn't move) ' [Buttons] The mouse button events you wish to send to the new cursor position ' [UseAbsolute] The mouse button events you wish to send to the new cursor position 'Outputs : N/A 'Notes : Can send absolute positions using the MOUSEEVENTF_ABSOLUTE (note, must divide VB coordinates by Screen.TwipsPerPixel) Sub MouseMove(Optional dX As Long = 0, Optional dY As Long = 0, Optional Buttons As eButtons = eNothing, Optional UseAbsolute As Boolean = False) Const MOUSEEVENTF_ABSOLUTE = &H8000, MOUSEEVENTF_LEFTDOWN = &H2, MOUSEEVENTF_LEFTUP = &H4 Const MOUSEEVENTF_MIDDLEDOWN = &H20, MOUSEEVENTF_MIDDLEUP = &H40, MOUSEEVENTF_MOVE = &H1 Const MOUSEEVENTF_RIGHTDOWN = &H8, MOUSEEVENTF_RIGHTUP = &H10 Dim lCoordBit As Long If UseAbsolute Then 'Add the bit for using absolute coordinates lCoordBit = MOUSEEVENTF_ABSOLUTE End If Select Case Buttons Case eNothing 'Just move the mouse mouse_event MOUSEEVENTF_MOVE Or lCoordBit, dX, dY, 0&, 0& Case eRightClick 'Move the mouse mouse_event MOUSEEVENTF_MOVE Or lCoordBit, dX, dY, 0&, 0& DoEvents 'Send a right click mouse_event MOUSEEVENTF_RIGHTDOWN Or lCoordBit, 0&, 0&, 0&, 0& mouse_event MOUSEEVENTF_RIGHTUP Or lCoordBit, 0&, 0&, 0&, 0& Case eDoubleRight 'Move the mouse mouse_event MOUSEEVENTF_MOVE Or lCoordBit, dX, dY, 0&, 0& DoEvents 'Send a right click mouse_event MOUSEEVENTF_RIGHTDOWN Or lCoordBit, 0&, 0&, 0&, 0& mouse_event MOUSEEVENTF_RIGHTUP Or lCoordBit, 0&, 0&, 0&, 0& DoEvents 'Send a right click mouse_event MOUSEEVENTF_RIGHTDOWN Or lCoordBit, 0&, 0&, 0&, 0& mouse_event MOUSEEVENTF_RIGHTUP, 0&, 0&, 0&, 0& Case eLeftClick 'Move the mouse mouse_event MOUSEEVENTF_MOVE Or lCoordBit, dX, dY, 0&, 0& DoEvents 'Send a left click mouse_event MOUSEEVENTF_LEFTDOWN Or lCoordBit, 0&, 0&, 0&, 0& mouse_event MOUSEEVENTF_LEFTUP Or lCoordBit, 0&, 0&, 0&, 0& Case eDoubleLeft 'Move the mouse mouse_event MOUSEEVENTF_MOVE Or lCoordBit, dX, dY, 0&, 0& DoEvents 'Send a left click mouse_event MOUSEEVENTF_LEFTDOWN Or lCoordBit, 0&, 0&, 0&, 0& mouse_event MOUSEEVENTF_LEFTUP Or lCoordBit, 0&, 0&, 0&, 0& DoEvents 'Send a left click mouse_event MOUSEEVENTF_LEFTDOWN Or lCoordBit, 0&, 0&, 0&, 0& mouse_event MOUSEEVENTF_LEFTUP Or lCoordBit, 0&, 0&, 0&, 0& End Select End Sub 'Test Code Private Sub Command1_Click() Debug.Print "Clicked " & Now MouseMove 50 / Screen.TwipsPerPixelX, 50 / Screen.TwipsPerPixelY, eLeftClick End Sub