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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StephenJRCommented:
Don't think so, but there is a double-click event.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

StephenJRCommented:
I knew that would happen.
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
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

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
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
Rory ArchibaldCommented:
Nice. If simplicity is the criterion, then this should be reopened and points awarded to andrewssd3! :)
andrewssd3Commented:
Thanks Rory - but  I'm just happy to help with no reward... sob

Stuart
24CaratAuthor Commented:
Indeed nice. :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.