?
Solved

Recognize ctrl-click

Posted on 2011-10-05
11
Medium Priority
?
419 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:24Carat
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 24

Expert Comment

by:StephenJR
ID: 36916060
Don't think so, but there is a double-click event.
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 36916088
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
 

Author Comment

by:24Carat
ID: 36916098
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 24

Expert Comment

by:StephenJR
ID: 36916107
I knew that would happen.
0
 
LVL 1

Expert Comment

by:mannhardt
ID: 36916108
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36916137
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
 

Author Closing Comment

by:24Carat
ID: 36916203
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
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36916221
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36916259
Nice. If simplicity is the criterion, then this should be reopened and points awarded to andrewssd3! :)
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36916365
Thanks Rory - but  I'm just happy to help with no reward... sob

Stuart
0
 

Author Comment

by:24Carat
ID: 36916391
Indeed nice. :-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question