Key Ascii
Key Ascii
Key Ascii
html
In previous lessons, we have only learned how to trigger events or control program flow by clicking the mouse. In this lesson, you will
learn how to use the keyboard to trigger an event using the keyboard beside using the mouse. When the user presses a key on the
keyboard, it will trigger an event or a series of events. These events are called the keyboard events. In Visual Basic, the three basic
event procedure to handle the keyboard events are KeyPress, Keydown and KeyUp
38.1 ASCII
The keyboard event occurs when the user presses any key that corresponds to a certain alphanumeric value or an action such as
Enter, spacing, backspace and more. Each of those value or action is represented by a set of code known as the ASCII . ASCII stands
for American Standard Code for Information Interchange.
ASCII stands for American Standard Code for Information Interchange. Computers can only understand numbers, so an ASCII code is
the numerical representation of a character such as 'a' or '@' or an action of some sort. ASCII was developed a long time ago and now
the non-printing characters are rarely used for their original purpose. In order to write code for the Keyboard events , we need to know
the ASCII and the corresponding values. Some of the common ASCII values are shown in Table 38.1.
8 Backspace 61 = 98 b
Carriage Return or
13 62 > 99 c
Enter key
32 Space 63 ? 100 d
33 ! 64 @ 101 e
34 " 65 A 102 f
1 of 6 14-10-2018, 09:01 AM
Keyboard Hnadling in Visual Basic https://www.vbtutor.net/lesson38.html
≡
37 % 68 D 105 i
38 & 69 E 106 j
39 ' 70 F 107 k
40 ( 71 G 108 l
41 ) 72 H 109 m
42 * 73 I 110 n
43 + 74 J 111 o
44 , 75 K 112 p
45 - 76 L 113 q
46 . 77 M 114 r
47 / 78 N 115 s
48 0 79 O 116 t
49 1 80 P 117 u
50 2 81 Q 118 v
51 3 82 R 119 w
52 4 83 S 120 x
53 5 84 T 121 y
54 6 85 U 122 z
55 7 86 V 123 {
56 8 87 W 124 |
57 9 88 X 125 }
58 : 89 Y 126 ~
59 ; 90 Z 127 DEL
60 < 97 a
vbKey0 48 0 vbKeyR 82 R
2 of 6 14-10-2018, 09:01 AM
Keyboard Hnadling in Visual Basic https://www.vbtutor.net/lesson38.html
≡
vbKey2 50 2 vbKeyT 84 T
vbKey3 51 3 vbKeyU 85 U
vbKey4 52 4 vbKeyV 86 V
vbKey5 53 5 vbKeyW 87 W
vbKey6 54 6 vbKeyX 88 X
vbKey7 55 7 vbKeyY 89 Y
vbKey8 56 8 vbKeyZ 90 Z
3 of 6 14-10-2018, 09:01 AM
Keyboard Hnadling in Visual Basic https://www.vbtutor.net/lesson38.html
≡
vbKeyL 76 L
vbKeyM 77 M
vbKeyN 78 N
vbKeyO 79 O
vbKeyP 80 P
vbKeyQ 81 Q
We can write code for the three key events i.e. keyPress, KeyDown and KeyUp.
Example 38.1
In this example, the program can detect the pressing of Enter key and the keys other than the Enter key.
Example 38.2
If you wish to detect and display the key pressed by the user, simply type the following code:
The function Chr will convert the ASCII values to the corresponding characters as shown in the ASCII table.
4 of 6 14-10-2018, 09:01 AM
Keyboard Hnadling in Visual Basic https://www.vbtutor.net/lesson38.html
≡
For i = 65 To 90
Print Chr(KeyAscii)
Next
End Sub
In this example, we use the For ...Next loop to display the alphabet A to Z by pressing any key on the keyboard.
Example 38.4
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
For i = 97 To 122
Print Chr(i)
Next
End If
End Sub
Hostgator - Linux Hosting simple calculator created Download PDF Tic Tac Toe Game Created
using Visual Baic 6 with Visual Basic
Advanced Calculator Visual Basic Sample Codes Create an electronic library Projectile simulation
created using visual basic in Visual Basic program creaated with
6 Visual Basic
vbtutor.net vbtutor.net vbtutor.net vbtutor.net
5 of 6 14-10-2018, 09:01 AM
Keyboard Hnadling in Visual Basic https://www.vbtutor.net/lesson38.html
6 of 6 14-10-2018, 09:01 AM