Key Press event

khurram7x

Registered User.
Local time
Today, 20:32
Joined
Mar 4, 2015
Messages
226
Hi,

How could i capture Enter and Esc on a KeyPress event on an open form please?

Thanks
 
You can look up the ASCII codes on line like here for example but it's just easier to use Debug.Print as in the following code to find out what they are. If you press Escape and Return you will see in the Immediate window that they have codes 27 and 13. For the KeyPress event to fire Key Preview needs to be set to Yes. It's way down at the bottom of the Form Properties Event tab.

Code:
Private Sub Form_KeyPress(KeyAscii As Integer)

Debug.Print KeyAscii

If KeyAscii = 27 Then
    MsgBox "Escape was pressed"
End If


If KeyAscii = 13 Then
    MsgBox "Enter was pressed"
End If


End Sub
 

Users who are viewing this thread

Back
Top Bottom