Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Vbe left-click with / without ctrl pressed down

Status
Not open for further replies.

atferrari

Well-Known Member
Most Helpful Member
Visual Basic for Excel.

I am not asking about shortcuts.

I need to get different actions for the same command button in a toolbar when:

I left-click on it.
I left-click on it while CTRL key is pressed down.

Edit/ The button has a macro assigned to it /Edit

How to distinguish between both situations?

My wording for searching failed: I end always reading about shortcuts.
 
Last edited:
I'm not sure how you would do that. Assuming that you've added a button to the toolbar then you can only send it to a subroutine without any key info. Could you just add two buttons?

Mike.
 
Hola Pommie,

Thanks for replying.

There are much too many buttons (49 in a custom toolbar). For every one I plan to launch a macro. If CTRL is pressed I decrease a certain variable. If CTRL isn't pressed, I increase it.

I presume it should be somehow related to the keycode but I still do not know how to get it...

Maybe after at a kind of "press down" event. Have to search for that.

Gracias.
 
hi at,
Are you asking about the SENDKEYS statement in Visual Basic,??
You can precede a Key code with Control key code to select an alternative action.
E
 
In fact Eric, I need to find the way to know if CTRL is pressed or not, once a left-click happens so I increase or decrease a variable inside the assigned macro.

Besides the mouse, only the CTRL key is involved.
 
You could use the windows API function.

Try

Code:
    Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

    Dim temp As Integer
    temp = GetKeyState(vbKeyControl)
    If temp = -127 Or temp = -128 Then MsgBox ("Control Pressed")

Edit, managed to try this now and it works fine.

Mike.
 
Last edited:
Gracias Pommie, late in the night I found the right wording: "testing CTRL key" and a solution similar to yours.

Code:
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Const VK_CONTROL As Integer = &H11 'Ctrl


Sub test()
        If GetKeyState(VK_CONTROL) < 0 Then Ctrl = True Else Ctrl = False

        If Ctrl = True Then
                Range("H7").Select
                ActiveCell.Value = "apretada"
        Else
                Range("H7").Select
                ActiveCell.Value = "sin apretar"
        End If
End Sub

One day I will try to understand why they need to deal with negative values to test keys. Certainly not intuitive.

Gracias again.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top