MouseDown, MouseUp Events — Event Procedures

Description

To create an event procedure that runs when the MouseDown or MouseUp event occurs, set the OnMouseDown or OnMouseUp property to [Event Procedure], and click the Build button.

Syntax

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

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

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

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

The MouseDown and MouseUp events have the following arguments.

Argument

Description

controlname

The name of the control whose MouseDown or MouseUp event procedure you want to run.

Button

The button that was pressed (MouseDown) or released (MouseUp) to trigger the event. 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.


(continued)

Argument

Description

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 for the current location of the mouse pointer. The X and Y arguments are always expressed in twips.


Note If a mouse button is pressed while the pointer is over a form or control, that object receives all mouse events up to and including the last MouseUp event. This means that the X and Y arguments may not always be relative to the object that receives them.

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 an event 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 used for the MouseMove event. For the MouseDown and MouseUp events, the Button argument indicates exactly one button per event; that is, if you press two mouse buttons, two MouseDown (and MouseUp) events will occur, each with a different Button argument setting. For the MouseMove event, the Button argument indicates the current state of all buttons.

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.

You can't cancel the MouseDown or MouseUp events.

See Also   MouseDown, MouseUp events — macros.

Example

The following example shows how you can find out which mouse button caused a MouseDown event.

To try the example, add the following event procedure to a form:

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, _
        X As Single, Y As Single)
    If Button = acLeftButton Then
        MsgBox "You pressed the left button."
    End If
    If Button = acRightButton Then
        MsgBox "You pressed the right button."
    End If
    If Button = acMiddleButton Then
        MsgBox "You pressed the middle button."
    End If
End Sub