Recognize ctrl-click

Hi Everybody,

Is it possible to have on my VBA userform in Excel a button which only works when users ctrl-clicks the button?

So if the user clicks without holding down ctrl button it won't work?

Regards,
24Carat
24CaratAsked:
Who is Participating?
 
Rory ArchibaldCommented:
Yes:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub CommandButton1_Click()
   If GetAsyncKeyState(vbKeyControl) <> 0 Then
      MsgBox "clicked with Ctrl key"
   End If
End Sub

Open in new window


for example.

HTH
Rory
0
 
StephenJRCommented:
Don't think so, but there is a double-click event.
0
 
24CaratAuthor Commented:
I've found this function but i can't seem to be able to integrate it.

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

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
StephenJRCommented:
I knew that would happen.
0
 
mannhardtCommented:
Here you go. CTRL-click.xls
It's a simple solution which will work for most situations, for a bulletproof code you need to use API functions to check the key state
0
 
GrahamSkanRetiredCommented:
You can test the status of the control key in the click event
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

Open in new window

0
 
24CaratAuthor Commented:
Waaw,

Never had so much reactions in such a short time.

They all work but i won't to give the credits to you Rorya because it is the simplest sollutions.

Regards,
24Carat
0
 
andrewssd3Commented:
Obviously not for points as it's now accepted, but you can do it without using api functions if you use the MouseUp event instead of the Click event:
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

Open in new window

ctrl-click.xlsm
0
 
Rory ArchibaldCommented:
Nice. If simplicity is the criterion, then this should be reopened and points awarded to andrewssd3! :)
0
 
andrewssd3Commented:
Thanks Rory - but  I'm just happy to help with no reward... sob

Stuart
0
 
24CaratAuthor Commented:
Indeed nice. :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.