Discussion Locked

What key was pressed?

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I want to know how I can find out what key was pressed when a control is clicked.
Example: The z key is pressed when cmdOK is clicked a message box pops up with the caption "You pressed the z key".
what variable or Event property will tell me whatkey is pressed

5 replies

by craig shepherd

Have you tried the KeyPress event for the form.

Put the following code in your form and see if it does what you want.

'Tells the user what key was pressed
Private Sub Form_KeyPress(KeyAscii As Integer)
MsgBox Chr$(KeyAscii)
End Sub

Ihope this helps

by mohammad_m_saggaf

first you have to set the form's property KeyPreview to true. then handle the event KeyPress, KeyUp, or KeyDown. As folloing:

Private Sub Form_Load()
Form1.KeyPreview = True
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
MsgBox "You pressed the " & Chr$(KeyAscii) & " key.", vbInformation, "Key pressed"
End Sub

I hope I helped. :-)

This discussion is closed to new replies.