• Check status of Alt Key (VBA)

    Author
    Topic
    #362292

    I have the following declaration and function combination which I find very useful. As you see they enable me to detect the state of the Shift and Ctrl keys at the time the macro was called and to change the behaviour accordingly.

    However I cannot find any way to enhance this function to detect the state of the Alt key. There is no constant vbKeyAlt.

    Is there a way of getting VBA to detect the status of the Alt key?

    Thanks.

    Jim Brook
    JBConsulting

    Private Declare Function GetKeyState Lib “user32” (ByVal vKey As Long) As Integer

    Public Function jbGetCurrentKeyState(lngKey As Long) As Boolean
    Select Case lngKey
    Case vbKeyShift
    jbGetCurrentKeyState = GetKeyState(vbKeyShift) < 0
    Case vbKeyControl
    jbGetCurrentKeyState = GetKeyState(vbKeyControl) < 0
    End Select
    End Function

    Viewing 0 reply threads
    Author
    Replies
    • #549798

      That was tough! Looks like it’s 18 or vbKeyMenu. Just in case you wonder how I found this, here was my clue. Search for Alt. Hope this works! –Sam

      • #549905

        Thanks for your help.

        Unfortunately the goal has not been achieved because I’ve just found out that when one pushes a toolbar button when Alt is pressed then the attached macro doesn’t run.

        I’ve given myself one more option however by checking for Ctrl and Shift both down as below.

        Thanks again.

        Public Function jbGetCurrentKeyState(lngKey As Long) As Boolean
        Select Case lngKey
        Case vbKeyShift
        jbGetCurrentKeyState = GetKeyState(vbKeyShift) < 0
        Case vbKeyControl
        jbGetCurrentKeyState = GetKeyState(vbKeyControl) < 0
        Case jbC_CTRL_PLUS_SHIFT
        jbGetCurrentKeyState = (GetKeyState(vbKeyShift) < 0) And GetKeyState(vbKeyControl)
        End Select
        End Function

    Viewing 0 reply threads
    Reply To: Check status of Alt Key (VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: