Results 1 to 5 of 5
  1. #1
    Plutonium Lounger Leif's Avatar
    Join Date
    Dec 2000
    Location
    U.K.
    Posts
    14,010
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Detect Shift key when running macro (2000 up)

    Is there an easy way to detect the Shift or Control key is pressed when running a macro?

    I'm using <code>Worksheet_BeforeDoubleClick</code> and it would be nice to be able to run alternative code if practical.

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Detect Shift key when running macro (2000 up)

    You can use something like this:
    <pre>Declare Function GetKeyState Lib "user32" _
    (ByVal nVirtKey As Long) As Integer
    Public Const VK_SHIFT = &H10
    Sub testshift()
    If (GetKeyState(VK_SHIFT) And &HF0000000) Then
    MsgBox "Shift is pressed"
    Else
    MsgBox "Shift is not pressed"
    End If
    End Sub
    </pre>

    to check the state of the shift key, but if you hold Shift or Ctrl then your BeforeDoubleClick event won't fire anyway!
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Detect Shift key when running macro (2000 up)

    Forgot to say that Alt will work with the DoubleClick, so you could use:
    <pre>Declare Function GetKeyState Lib "user32" _
    (ByVal nVirtKey As Long) As Integer
    Public Const VK_SHIFT = &H10
    Const VK_MENU = &H12
    Sub testshift()
    If (GetKeyState(VK_MENU) And &HF0000000) Then
    MsgBox "Alt is pressed"
    Else
    MsgBox "Alt is not pressed"
    End If
    End Sub
    </pre>


    HTH
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Detect Shift key when running macro (2000 up)

    As Rory notes, BeforeDoubleClick doesn't occur if you hold down Ctrl or Shift. This is because these modifiers are used to extend the selection.
    You can detect the Alt key, however:

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If GetKeyState(vbKeyMenu) And &H8000 Then
    MsgBox "Alt key down", vbInformation
    End If
    ' Optional - cancel default action
    Cancel = True
    End Sub

  5. #5
    Plutonium Lounger Leif's Avatar
    Join Date
    Dec 2000
    Location
    U.K.
    Posts
    14,010
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detect Shift key when running macro (2000 up)

    Thanks both - I have something to play around with now <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

Posting Permissions

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