Public Function IsControlKeyDown(Optional LeftOrRightKey As Long = LeftKeyOrRightKey) As Boolean
''''''''''''''''''''''''''''''''''''''''''''''''
' IsControlKeyDown
' Returns TRUE or FALSE indicating whether the
' CTRL key is down.
'
' If LeftOrRightKey is omitted or LeftKeyOrRightKey,
' the function return TRUE if either the left or the
' right CTRL key is down. If LeftKeyOrRightKey is
' LeftKey, then only the Left CTRL key is tested.
' If LeftKeyOrRightKey is RightKey, only the Right
' CTRL key is tested. If LeftOrRightKey is
' BothLeftAndRightKeys, the codes tests whether
' both the Left and Right keys are down. The default
' is to test for either Left or Right, making no
' distiction between Left and Right.
''''''''''''''''''''''''''''''''''''''''''''''''
Dim Res As Long
Select Case LeftOrRightKey
Case LeftKey
Res = GetKeyState(VK_LCTRL) And KEY_MASK
Case RightKey
Res = GetKeyState(VK_RCTRL) And KEY_MASK
Case BothLeftAndRightKeys
Res = (GetKeyState(VK_LCTRL) And GetKeyState(VK_RCTRL) And KEY_MASK)
Case Else
Res = GetKeyState(vbKeyControl) And KEY_MASK
End Select
IsControlKeyDown = CBool(Res)
End Function
Option Explicit
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Const VK_CONTROL = &H11
Private Sub CommandButton1_Click()
Dim state As Integer
state = GetKeyState(VK_CONTROL)
If state And &H8000 Then
MsgBox "Contol key pressed"
Else
MsgBox "Contol key not pressed"
Exit Sub
End If
'rest of code
'...
End Sub
Private Sub CommandButton1_Click()
' no action
End Sub
Private Sub CommandButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 1 Then ' left button
If Shift And 2 Then ' Ctrl was pressed
MsgBox "You got it right!"
End If
End If
End Sub