mouse_event Function

Declare Sub mouse_event Lib "user32.dll" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

Platforms

Description & Usage

mouse_event synthesizes mouse input by placing mouse input information into the input stream. A single mouse input event consists of either a move of the mouse or the change of the button state. For mouse movement, the coordinates can be given in either absolute or relative form. Only changes in mouse position or button state should be send via this function. For example, if the left mouse button is already down, the program should not send another left-button-down input.

Return Value

mouse_event does not return a value.

Visual Basic-Specific Issues

None.

Parameters

dwFlags
A combination of the following flags specifying which mouse input information to place into the input stream. Remember to only specify button status information for those which have changed. Note that scroll wheel movement and X button status cannot be simultaneously specified because they both use the dwData parameter.
MOUSEEVENTF_ABSOLUTE
The dx and dy parameters contain absolute mouse coordinates. In the coordinate system used by the function, the screen's upper-left corner has coordinates (0,0) and the lower-right corner has coordinates (65535,65535), regardless of the actual screen size. If this flag is not set, dx and dy contain relative coordinates, whose actual amount of movement depends on the current mouse speed and acceleration settings.
MOUSEEVENTF_LEFTDOWN
The left button was pressed.
MOUSEEVENTF_LEFTUP
The left button was released.
MOUSEEVENTF_MIDDLEDOWN
The middle button was pressed.
MOUSEEVENTF_MIDDLEUP
The middle button was released.
MOUSEEVENTF_MOVE
The mouse moved. The dx and dy parameters specify the amount or location of the movement.
MOUSEEVENTF_RIGHTDOWN
The right button was pressed.
MOUSEEVENTF_RIGHTUP
The right button was released.
MOUSEEVENTF_WHEEL
Windows NT, 2000: The scroll wheel has moved. The dwData parameter specifies the amount of movement.
MOUSEEVENTF_XDOWN
Windows 2000: An X button was pressed. The dwData parameter identifies which X buttons.
MOUSEEVENTF_XUP
Windows 2000: An X button was released. The dwData parameter identifies which X buttons.
dx
Specifies either the x-coordinate of absolute mouse movement or the amount of relative movement along the x-axis. For relative motion, positive values move right and negative values move left.
dy
Specifies either the y-coordinate of absolute mouse movement or the amount of relative movement along the y-axis. For relative motion, positive values move down and negative values move up.
dwData
Windows NT, 2000: If dwFlags contains MOUSEEVENTF_WHEEL, this specifies the amount of wheel movement, in integer multiples of WHEEL_DELTA. Positive values mean forward (away) rotation, and negative values mean backwards (toward) rotation. Windows 2000: If dwFlags contains either MOUSEEVENTF_XDOWN or MOUSEEVENTF_XUP, this is a combination of the following flags specifying which X buttons have been pressed or released:
XBUTTON1
The first X button was pressed or released.
XBUTTON2
The second X button was pressed or released.
dwExtraInfo
An additional 32-bit value associated with the mouse event.

Constant Definitions

Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10
Const MOUSEEVENTF_WHEEL = &H80
Const MOUSEEVENTF_XDOWN = &H100
Const MOUSEEVENTF_XUP = &H200
Const WHEEL_DELTA = 120
Const XBUTTON1 = &H1
Const XBUTTON2 = &H2

Example

Simulate clicking the left mouse button at the upper-left corner of the screen. Although we could use the mouse_event function to move the cursor, it is much easier to use SetCursorPos instead. The example runs when the user clicks button Command1. Obviously, to use this example, you must place a command button named Command1 on a form window.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Declare Sub mouse_event Lib "user32.dll" (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.dll" (ByVal x As Long, ByVal y As Long) As Long

Private Sub Command1_Click()
	Dim retval As Long  ' return value
	
	' Move the mouse cursor to the upper-left corner of the screen.
	retval = SetCursorPos(0, 0)
	' Click the left mouse button once.
	mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
	mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

See Also

keybd_event, SendInput

Category

Mouse

Go back to the Function listing.
Go back to the Reference section index.


Last Modified: August 26, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/m/mouse_event.html