VBE left-click with / without CTRL pressed down

Discussion in 'Programmer's Corner' started by atferrari, Oct 7, 2013.

  1. atferrari

    Thread Starter AAC Fanatic!

    Jan 6, 2004
    2,648
    764
    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 command 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: Oct 7, 2013
  2. Alec_t

    AAC Fanatic!

    Sep 17, 2013
    5,804
    1,105
    Hi Agustin. Try searching for 'key codes'.
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    It also helps if you start your search with the letters "VBA" which stands for Visual Basic for Applications.
     
  4. atferrari

    Thread Starter AAC Fanatic!

    Jan 6, 2004
    2,648
    764
    Again my thanks to you both for replying.

    Problem solved. The right wording is "testing CTRL key"

    Found this amongst a few similar solutions.


    Code ( (Unknown Language)):
    1.  Declare Function GetKeyState Lib "user32" _
    2. (ByVal nVirtKey As Long) As Integer
    3.  
    4. Const VK_CONTROL As Integer = &H11 'Ctrl
    5.  
    6.  
    7. Sub test()
    8.          If GetKeyState(VK_CONTROL) < 0 Then Ctrl = True Else Ctrl = False
    9.  
    10.          If Ctrl = True Then
    11.                  Range("H7").Select
    12.                  ActiveCell.Value = "apretada"
    13.      Else
    14.                  Range("H7").Select
    15.                  ActiveCell.Value = "sin apretar"
    16.              End If
    17. End Sub
    18.  
     
Loading...