Programmer's Perspective ] Ins and Outs ] WhereCode ] Typology ] Alphabetic Information ] Arrays and Looping ] Builtins ] Randomization and Simulation ] Controls ] Creating and Using Files ] Errors ] [ Key Codes ] Drive Office With VB ]

 

 


Key Codes

Every key on the keyboard has associated with it a numerical code value. These values can be used in your program for two primary purposes. One purpose is to allow the user to type acceptable, and only acceptable, characters that will be accepted by controls. The second purpose is to allow the user to perform keyboard operations such as backspacing and deleting in a natural way. 

My most frequent need for these codes arises when I employ the grid control to accept data input. The keyboard strokes are intercepted before anything visible occurs. Most of the keys are intercepted via code in the KeyPress subroutine. The KeyPress subroutine fires when the user presses a key while the control has the focus. However, VB wouldn't be a Microsoft product if it didn't have the peculiar "feature" that some keys have to be intercepted via code in the KeyDown subroutine. The system checks KeyDown before it checks KeyPress.

Checking is accomplished with either an If block or a Select Case block. The latter is generally preferable, because there are a lot of keys the user might hit, in some instances accidentally, and we want the program to handle all contingencies.

For example, here's some code from a Grid KeyPress subroutine in which the goal is for the user to type one or more digits in a cell. Unlike Excel, the grid does not automatically display what the user types; there must be code that makes the keystrokes visible.

Private Sub Grid1_KeyPress(KeyAscii As Integer)
 Select Case KeyAscii
     Case 43 To 46, 48 To 57   'acceptable characters
          Grid1.Text = Grid1.Text + Str(KeyAscii)
     Case 8   'backspace key
          Grid1.Text = Left(Grid1.Text, Len(Grid1.Text)-1)
     Case 13    'enter key - move to the next cell
         If Grid1.Row < Grid1.Rows - 2 then
             Grid1.Row = Grid1.Row + 1  
         End if
      Case Else   'nothing happens if the character is unacceptable
 End Select     
End Sub 

         
And here's the accompanying code, using KeyDown, that allows the Delete key to function as the user would expect it to. Note that the parameter for the subroutine has a different name, and that Keycode values do not correspond to KeyAscii values..

Private Sub Grid1_KeyDown(KeyCode As Integer, Shift As Integer)

      If KeyCode = 46 then   'delete key
         Grid1.Text = ""
      End If
End Sub

Here are some of the Ascii codes that work in KeyPress. For a more extensive listing, see the Ansi Character Codes Chart in MSDN help.

Key Ascii

What the user thinks it does

8 Backspace
10 Line Feed (Shift + Enter)
13 Enter (Carriage Return)
32 Space
33 ! (Exclamation point)
34 " (Quote)
35 #
36 $
37 %
38 &
39 '  (apostrophe)
40 (
41 )
42 *  (asterisk)
43 +
44 ,   (comma)
45 -  (minus sign or dash)
46 .  (period or decimal point)
47 /
48 to 57 0 to 9
58 :  (colon)
59 ;  (semi-colon)
60 <
61 =
62 >
63 ?
64 @
65 to 90 A to Z (capital letters)
91 [
92 \
93 ]
94 ^
95 _
96 `
97 to 122 a to z (small letters)
123 {
124 |
125 }
126 ~
169 ©  (copyright symbol)
177 ±
188 ¼
189 ½
190 ¾


And here are some of the KeyCodes that work in Key Down:

KeyCode

What the user thinks it does

33 Page Up
34 Page Down
35 End
36 Home
37 Left arrow
38 Up arrow
39 Right arrow
40 Down arrow
45 Insert
46 Delete