VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 3

Determining the Mouse Button

After the desired menus have been created, the next step for the programmer is to decide which objects will call which menus. The form, as well as individual controls on the form, can all have pop-up menus specified.

The standard method used to activate an object's pop-up menu is to use the MouseUp event procedure to detect when the user right-clicks the mouse. After the user right-clicks, the menu is displayed.

To determine the state of the mouse, the programmer traps the MouseUp event of the object to provide the menu. Using the event's Button parameter, the programmer verifies which mouse button the user pressed.

To determine which mouse button was activated, the procedure is passed two arguments: Button and Shift. The Button argument indicates the mouse button that was pressed. The Shift indicates whether the Ctrl and/or Alt and/or Shift was active when the mouse click event occurred.

The following code in the form's MouseUp event determines which mouse button was pressed and then further distinguishes which Shift key or Shift key combination was pressed along with the mouse button.

Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim blnIsAlt as Boolean
Dim blnIsCtrl as Boolean
Dim blnIsShift as Boolean
BlnIsAlt = Shift And vbAltMask
BlnIsCtrl = Shift And vbCtrlMask
BlnIsShift = Shift And vbShiftMask
If Button = vbLeftButton Then
If blnIsAlt And blnIsShift Then
… So something to react to Left Button +
Alt + Shift
End If
ElseIf Button = vbRightButton Then
Form1.PopupMenu mnuPopUp1
End If
End Sub

The preceding code uses VB constants to determine the button-Shift key combination. The key combination of Ctrl and/or Alt and/or Shift will also be returned as a number in the Shift parameter. You can test to see whether a particular key was pressed by the user by using the AND operator to compare the Shift parameter with one of the bit mask constants vbAltKey, vbShiftKey, or vbCtrlKey.

In the MouseUp event, both Button and Shift are integer values. When programming for this event, either the integer value can be used or the VB constants that refer to the various possible Button and Shift key values.

For further discussion of how to program with the MouseUp and MouseDown event procedures, see the section in this chapter entitled "MouseUp and MouseDown."


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved