MouseMove Event — Event Procedures

Description

To create an event procedure that runs when the MouseMove event occurs, set the OnMouseMove property to [Event Procedure], and click the Build button.

Syntax

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single,
úY As Single)

Private Sub controlname_MouseMove(Button As Integer, Shift As Integer,
úX As Single, Y As Single)

The MouseMove event has the following arguments.

Argument

Description

controlname

The name of the control whose MouseMove event procedure you want to run.

Button

The state of the mouse buttons when the event occurs. If you need to test for the Button argument, you can use one of the following intrinsic constants as bit masks:

Constant

Description

acLeftButton

The bit mask for the left mouse button.

acRightButton

The bit mask for the right mouse button.

acMiddleButton

The bit mask for the middle mouse button.

Shift

The state of the SHIFT, CTRL, and ALT keys when the button specified by the Button argument was pressed or released. If you need to test for the Shift argument, you can use one of the following intrinsic constants as bit masks:

Constant

Description

acShiftMask

The bit mask for the SHIFT key.

acCtrlMask

The bit mask for the CTRL key.

acAltMask

The bit mask for the ALT key.

X, Y

The x and y coordinates of the current location of the mouse pointer. The X and Y arguments are always expressed in twips.


Remarks

You test for a condition by first assigning each result to a temporary Integer variable and then comparing the Shift or Button argument to an intrinsic constant. Use the And operator with the Button argument to test whether the condition is greater than 0, indicating that the left, middle, or right mouse button was pressed, as in the following example:

LeftDown = (Button And acLeftButton) > 0
In a procedure, you can test for any combination of conditions, as in the following example:

If ShiftDown And CtrlDown Then
    .                    ' Do this if SHIFT and CTRL keys are pressed.
    .
    .
End If
Note The Button argument for the MouseDown and MouseUp events differs from the Button argument for MouseMove. For the MouseMove event, the Button argument indicates the current state of all buttons; a single MouseMove event can indicate that some, all, or no button is pressed. For example, if you press both the left and right buttons while moving the mouse, the Button argument is set to 3 (1 + 2). For the MouseDown and MouseUp events, the Button argument indicates one button per event.

You can use the arguments for the KeyDown, KeyPress, and KeyUp events, in conjunction with the arguments for the MouseDown, MouseUp, and MouseMove events, to make your application work smoothly for both keyboard and mouse users. For example, you may need to coordinate the keyboard focus with the mouse focus. Normally, these are the same. For example, if you move the mouse pointer to a text box and click the mouse button, both the keyboard focus and the mouse focus move to the text box. However, moving the mouse pointer over a control does not move the keyboard focus to that control. You need to click a mouse button, use one of the keyboard navigation keys, or set the focus to the control in the MouseMove event procedure.

Since the MouseMove event occurs for each area of the form it moves over, you can end up with a multitude of MouseMove events if you don't control them. You may want to make sure that if a control or section has received a MouseMove event, the event doesn't occur for the control or section again while the mouse pointer is still on this area of the form. To do this, set and check a module variable that indicates whether the MouseMove event has already occurred for this control or form section.

You can't cancel the MouseMove event.

See Also

MouseMove event — macros.

Example

The following example determines where the mouse is and whether the left mouse button and/or the SHIFT key is pressed. The x and y coordinates of the mouse pointer position are displayed in a label control as you move the mouse.

To try the example, add the following event procedure to a form that contains a label named Coordinates:

Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, _
        X As Single, Y As Single)
    Dim intShiftDown As Integer, intLeftButton As Integer

    Me!Coordinates.Caption = X & ", " & Y
    ' Use bit masks to determine state of SHIFT key and left button.
    intShiftDown = Shift And acShiftMask
    intLeftButton = Button And acLeftButton
    ' Check that SHIFT key and left button are both pressed.
    If intShiftDown And intLeftButton > 0 Then
        MsgBox "Shift key and left mouse button were pressed."
    End If
End Sub