Using Button with MouseMove

For the MouseMove event, button indicates the complete state of the mouse buttons — not just which button caused the event, as with MouseDown and MouseUp. This additional information is provided because all, some, or none of the bits might be set. This compares with just one bit per event in the MouseDown and MouseUp procedures.

Testing for a Single Button

If you test MouseMove for equality to 001 (decimal 1), you're testing to see if only the left mouse button is being held down while the mouse is moved. If another button is held down with the left button, the following code doesn't print anything:

Private Sub Form_MouseMove (Button As Integer, _
      Shift As Integer, X As Single, Y As Single)
   If Button = 1 Then Print "You're pressing _
      only the left button."
End Sub

To test for whether a particular button is down, use the And operator. The following code prints the message for each button pressed, regardless of whether another button is pressed:

Private Sub Form_MouseMove (Button As Integer, _
      Shift As Integer, X As Single, Y As Single)
   If Button And 1 Then Print "You're pressing _
      the left button."
   If Button And 2 Then Print "You're pressing _
      the right button."
End Sub

Pressing both buttons simultaneously prints both messages to the form. The MouseMove event recognizes multiple button states.

Testing for Multiple Buttons

In most cases, to isolate which button or buttons are being pressed, you use the MouseMove event.

Building on the previous examples, you can use the If…Then…Else statement to determine whether the left, right, or both buttons are being pressed. The following example tests for the three button states (left button pressed, right button pressed, and both buttons pressed) and prints the corresponding message.

Add the following code to the form's MouseMove event:

Private Sub Form_MouseMove(Button As Integer, _
      Shift As Integer, X As Single, Y As Single)
   If Button = 1 Then
      Print "You're pressing the left button."
   ElseIf Button = 2 Then
      Print "You're pressing the right button."
   ElseIf Button = 3 Then
      Print "You're pressing both buttons."
   End If
End Sub

You could also use the And operator with the Select Case statement to determine button and shift states. The And operator combined with the Select Case statement isolates the possible button states of a three-button mouse and then prints the corresponding message.

Create a variable called ButtonTest in the Declarations section of the form:

Dim ButtonTest as Integer

Add the following code to the form's MouseMove event:

Private Sub Form_MouseMove(Button As Integer, _
      Shift As Integer, X As Single, Y As Single)
ButtonTest = Button And 7
   Select Case ButtonTest
      Case 1 ' or vbLeftButton
         Print "You're pressing the left button."
      Case 2 ' or vbRightButton
         Print "You're pressing the right button."
      Case 4 ' or vbMiddleButton
         Print "You're pressing the middle button."
      Case 7
         Print "You're pressing all three buttons."
   End Select
End Sub