Avatar of 24Carat
Flag for Belgium asked on

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?

Visual Basic ClassicMicrosoft ExcelMicrosoft Office

Avatar of undefined
Last Comment

8/22/2022 - Mon

Don't think so, but there is a double-click event.
Rory Archibald

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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


I knew that would happen.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck

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

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"
    MsgBox "Contol key not pressed"
    Exit Sub
End If
'rest of code
End Sub

Open in new window



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.

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

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

Rory Archibald

Nice. If simplicity is the criterion, then this should be reopened and points awarded to andrewssd3! :)

Thanks Rory - but  I'm just happy to help with no reward... sob

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck

Indeed nice. :-)