VB2022 VB2019 VB6 VB Sample Codes About Us

Lesson 38: Keyboard Handling


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 Writing the 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.

Table 38.1: ASCII Values

ASCII
Chr
ASCII
Chr
ASCII
Chr
8 Backspace 61 = 98 b
13 Carriage Return or Enter key 62 > 99 c
32 Space 63 ? 100 d
33 ! 64 @ 101 e
34 " 65 A 102 f
35 # 66 B 103 g
36 $ 67 C 104 h
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    

For more detail table, please refer to  https://www.asciitable.com/

38.2 Common Key Events Constants.

In Visual Basic 6, it employs a set of constants that correspond to the ASCII values. We can use the constants instead of the ASCII. Table 38.2 shows the constants and the corresponding ASCII values.

Table 38.2: Common Key Events Constants

Event Constant
ASCII
Chr
Event Constant
ASCII
Chr

vbKey0

48

0

vbKeyR

82

R

vbKey1

49

1

vbKeyS

83

S

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

vbKey9

57

9

vbKeyDecimal

110

Decima point

vbKeyA

65

A

vbkeyBack

8

Backspace key

vbKeyB

66

B

vbKeyTab

9

Tab key

vbKeyC

67

C

vbkeyReturn

13

Return key(Enter key)

vbKeyD

68

D

vbKeyShift

16

Shift key

vbKeyE

69

E

vbKeyControl

17

Ctrl key

vbKeyF

70

F

vbKeyCapital

20

Caps Lock key

vbKeyG

71

G

vbKeyEscape

27

Esc key

vbKeyH

72

H

vbKeySpace

32

Space bar

vbKeyI

73

I

vbKeyInsert

45

Insert key

vbKeyJ

74

J

vbKeyDelete

46

Delete key

vbKeyK

75

K

 

 

 

vbKeyL

76

L

 

 

 

vbKeyM

77

M

 

 

 

vbKeyN

78

N

 

 

 

vbKeyO

79

O

 

 

 

vbKeyP

80

P

 

 

 

vbKeyQ

81

Q

 

 

 

38.3 Writing code for the key events

We can Writing the Code for the three key events i.e. keyPress, KeyDown and KeyUp.

Example 38.1

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then '13 is the ASCII value for the Enter key
 Print "You have pressed the Enter key"
Else
 Print "You have pressed other key"
End If
End Sub

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:

Private Sub Form_KeyPress(KeyAscii As Integer)
 Print Chr(KeyAscii)
End Sub

The function Chr will convert the ASCII values to the corresponding characters as shown in the ASCII table.

Example 38.3

Private Sub Form_KeyPress(KeyAscii As Integer)
 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



Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy