Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    Feb 2001
    Location
    Auckland, North Island, New Zealand
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check status of Alt Key (VBA)

    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

  2. #2
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Check status of Alt Key (VBA)

    That was tough! Looks like it's 18 or vbKeyMenu. Just in case you wonder how I found this, <A target="_blank" HREF=http://www.vbapi.com/ref/other/virtualkeycodes.html>here</A> was my clue. Search for Alt. Hope this works! --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  3. #3
    Star Lounger
    Join Date
    Feb 2001
    Location
    Auckland, North Island, New Zealand
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Check status of Alt Key (VBA)

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •