'in the KeyPress event of any control
If
KeyAscii
=
vbKeyReturn
Then
'KeyAscii does not recognize all keys, only those with Ascii equivalents
'this example finds the Enter Key (Ascii code 13)
'other key/ascii codes that KeyPress recognizes include:
'vbKeySpace (32) vbKeyBack (8) vbKeyTab (9)
'use KeyUp / KeyDown events to recognize other keys which do not
'have Ascii equivalents arrows, function keys,
MSGBox
"Enter was pressed!"
'after doing something, set KeyAscii to zero to prevent VB from doing anything else
KeyAscii
=
0
End If
'this is the same as using character code constants
If
KeyAscii
=
13
Then
MSGBox
"Enter was pressed"
End If
'KeyCode represents the physical key that is pressed/released (all keys except TAB)
'in the KeyUp events
If
KeyCode
=
vbKeyReturn
Then
MSGBox
"Enter key was released"
End If
'in the KeyDown events
If
KeyCode
=
vbKeyReturn
Then
MSGBox
"Enter key was pressed"
End If