Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

All - capture any key


'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

'other useful KeyCode constants include:
'vbKeyLButton vbKeyRButton
'vbKeyReturn
'vbKeyShift
'vbKeyControl
'vbKeyEscape
'vbKeySpace
'vbKeyUp vbKeyDown vbKeyLeft vbKeyRight
'vbKeyPageUp  vbKeyPageDown  vbKeyEnd  vbKeyHome
'vbKeyF1 - vbKey16