Using Button with MouseDown and MouseUp

You use the button argument with MouseDown to determine which button is being pressed and with MouseUp to determine which button has been released. Because only one bit is set for each event, you can't test for whether two or more buttons are being used at the same time. In other words, MouseDown and MouseUp only recognize one button press at a time.

Note   In contrast, you can use the MouseMove event to test for whether two or more buttons are being pressed simultaneously. You can also use MouseMove to test for whether a particular button is being pressed, regardless of whether or not another button is being pressed at the same time. For more information, see "Using Button with MouseMove" later in this chapter.

You can specify which button causes a MouseDown or MouseUp event with simple code. The following procedure tests whether button equals 1, 2, or 4:

Private Sub Form_MouseDown (Button As Integer, _
      Shift As Integer, X As Single, Y As Single)
   If Button = 1 Then Print "You pressed _
      the left button."
   If Button = 2 Then Print "You pressed _
      the right button."
   If Button = 4 Then Print "You pressed _
      the middle button."
End Sub

If the user presses more than one button, Visual Basic interprets that action as two or more separate MouseDown events. It sets the bit for the first button pressed, prints the message for that button, and then does the same for the next button. Similarly, Visual Basic interprets the release of two or more buttons as separate MouseUp events.

The following procedure prints a message when a pressed button is released:

Private Sub Form_MouseUp(Button As Integer, _
      Shift As Integer, X As Single, Y As Single)
   If Button = 1 Then Print "You released _
      the left button."
   If Button = 2 Then Print "You released _
      the right button."
   If Button = 4 Then Print "You released _
      the middle button."
End Sub