Sie sind auf Seite 1von 3

Key Codes

http://instructional1.calstatela.edu/dweiss/Psy409/key_codes.htm

[ 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 Grid1.Text = "" End If End Sub 'delete key

1 of 3

6/12/2013 10:09 PM

Key Codes

http://instructional1.calstatela.edu/dweiss/Psy409/key_codes.htm

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 8 10 13 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 to 57 58 59 60 61 62 63 64 65 to 90 91 92 93 94 95 96 Backspace Line Feed (Shift + Enter) Enter (Carriage Return) Space ! (Exclamation point) " (Quote) # $ % & ' (apostrophe) ( ) * (asterisk) + , (comma) - (minus sign or dash) . (period or decimal point) / 0 to 9 : (colon) ; (semi-colon) < = > ? @ A to Z (capital letters) [ \ ] ^ _ ` What the user thinks it does

2 of 3

6/12/2013 10:09 PM

Key Codes

http://instructional1.calstatela.edu/dweiss/Psy409/key_codes.htm

97 to 122 123 124 125 126 169 177 188 189 190

a to z (small letters) { | } ~ (copyright symbol)

And here are some of the KeyCodes that work in Key Down: KeyCode 33 34 35 36 37 38 39 40 45 46 Page Up Page Down End Home Left arrow Up arrow Right arrow Down arrow Insert Delete What the user thinks it does

3 of 3

6/12/2013 10:09 PM

Das könnte Ihnen auch gefallen