Determine Shift Key Press

  • Thread starter Thread starter Todd Huttenstine
  • Start date Start date
T

Todd Huttenstine

Hey guys

I want to be able to click CommandButton1 and if it
detects that I was holding the "Shift" key down during the
click, it give me a msgbox saying "Shift Key Pressed".

How do I do this?

Thank you
Todd Huttenstine
 
Todd

Where? On a sheet? Try this

Dim ShiftKeyPress As Boolean

Private Sub CommandButton1_Click()
If ShiftKeyPress Then
MsgBox "Shift key is pressed"
Else
MsgBox "Shift key is not pressed"
End If

End Sub

Private Sub CommandButton1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)

ShiftKeyPress = True
End Sub

Private Sub CommandButton1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)

ShiftKeyPress = False
End Sub
 
It works but after I press the shift key and then click,
it always tells me shift key was pressed on all other
clicks even if Shift was not pressed.
 
I added the last line to the click event and it works:

Private Sub CommandButton1_Click()
If ShiftKeyPress Then
MsgBox "Shift key is pressed"
Else
MsgBox "Shift key is not pressed"
End If
ShiftKeyPress = False
End Sub
 
Todd

You're right. If you release the Shift key after you click the message box,
it works OK. But if you release it while the message box is in focus, the
KeyUp doesn't fire.

Add

ShiftKeyPress = False

to the end of your click event. Then you will have the problem of the user
holding the shift key for two consecutive clicks. I think if you want this
to be rock solid, you'll need to use the API to hook the keyboard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top