The MouseDown event fires when
the user presses a mouse button over a control
or form. Similarly the MouseUp event occurs
when the user releases the mouse button over
a control or form. Note that if the user moves
the mouse between the time the button was pressed
and released, the same control (i.e., the control
that originally received the MouseDown) will
receive the MouseUp event.
During the MouseDown and MouseUp
event procedures, you might want to know whether
the left or right mouse button was pressed.
You might also want to know whether or not one
of the auxiliary keys (Shift, Alt, or Ctrl)
also was depressed during the mouse event.
Finally, it might be nice
to know the relative position of the mouse pointer
within the form or control receiving the event.
All of the foregoing information is available in parameters that the MouseUp
or MouseDown event procedure receives from the system. The four parameters are:
- Button As Integer. This
is a value representing which mouse button
fired the event. The value of this parameter
is either vbLeftButton, vbRightButton, or
vbMiddleButton. Again these terms are from
the point of view of a right-handed mouse.
vbLeftButton always refers to the primary
button, regardless of whether it's physically
the left or right button.
- Shift As Integer. This
parameter represents an integer that indicates
whether an auxiliary key is pressed during
the Mouse event. It contains a value of 0
(none), 1 (Shift), 2 (Ctrl), 4 (Alt), or the
sum of any combination of those keys. For
example, if both the Ctrl and Alt key were
pressed, the value of the Shift parameter
is 6. You can check for the state of any one
of the auxiliary keys with one of the VB constants
vbAltMask, vbCtrlMask, or vbShiftMask. The
following code illustrates how you could store
the state of each auxiliary key in a Boolean
variable within the MouseDown or MouseUp event
procedure. The bit-wise representation of
1, 2, or 4 in the Shift parameter is 000000001,
000000010, 00000100. By doing a logical AND
between the Shift parameter and one of the
VB Shift-key constants, you can pick out whether
each of the three Shift keys is currently
pressed, as illustrated in Listing 3.1.
- X As Single. This is the
horizontal position of the mouse pointer from
the internal left edge of the control or form
receiving the event.
- Y As Single. This is the vertical position of the mouse
pointer from the internal top edge of the control or form receiving the event.
TESTING THE SHIFT MASK IN A MOUSEDOWN OR MOUSEUP
Dim blnIsAlt As Boolean
Dim blnIsCtrl As Boolean
Dim blnIsShift As Boolean
blnIsAlt = Shift And vbAltMask
blnIsCtrl = Shift And vbCtrlMask
blnIsShift = Shift And vbShiftMask
"Of course," you may be thinking, "isn't a Click event simply the combination
of a MouseUp and a MouseDown?" How does the system handle this fact when the user
clicks or double-clicks the mouse? In the next section we discuss how a VB program
handles the combination of these various events.