Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect control+click event??? 1

Status
Not open for further replies.

darkangrydragon

IS-IT--Management
Jul 18, 2002
15
0
0
US
Hi everyone!

I was wondering how in VBA for access you can detect when the user clicks a command button while holding the control key (CTRL). I imagine this will have to be done in the command button's Click event, but how do you detect if the Control key is being held down while this event occurs?

-Andrew
 
Use the MouseDown event instead of the Click event.

Private Sub Command0_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (Shift And acShiftMask) Then
MsgBox "You held the shift key down", vbInformation
Else
MsgBox "You clicked the button", vbInformation
End If
End Sub

Defined bit masks are: acShiftMask, acCtrlMask & acAltMask. Use them in combination by ORing them together e.g. If (Shift And (acShiftMask Or acCtrlMask)) Then ... which will equate to True if Shift matches the mask as it will be non-zero.
 
Thanks, Norris!

How about this, though:

How do you detect a control+double click?

Thanks!

-Andrew
 
You could set the KeyPreview property True and create a Form_KeyDown & Form_KeyUp event handlers. Set a module-level flag when the shift key is pressed. Then test that value within the button DoubleClick event. You won't want to be displaying a message box in the MouseDown event anymore or this isn't going to work as double-click is never detected.

Code:
Dim ShiftPressed As Boolean

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
ShiftPressed = (Shift And acShiftMask)
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
ShiftPressed = False
End Sub

Private Sub Command8_DblClick(Cancel As Integer)
If ShiftPressed Then
    MsgBox "You double-clicked with shift. Give the man a star!", vbInformation
Else
    MsgBox "You double-clicked", vbInformation
End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top