Thursday, August 27, 2009

Keyboard Handling

So far most of the programs we have done here were using the command buttons which required mouse actions such as click or double click. However, we can also write the code that can be triggered using the keyboard.

Keyboard handling is relatively easy in VB6. There are basically only three events associated with the keyboard, i.e. KeyPress, KeyUp and KeyDown. You can write code for each of the aforementioned events. Before you can do that, you need to know the ASCII code as well as the chr function. ASCII stands for American Standard Code for Information Interchange. Each character or symbol on a keyboard is represented by a numerical code (ASCII code) which can be understood by the computer. It may be even an action such as Enter or Backspace. For a complete set of ASCII code, please refer to this link below:

http://www.asciitable.com/.

On the other hand, the Chr function is to convert an ASCII code into a meaningful character which can be undertood by a human being.

A standard VB code for handing keyboard is shown in the example below:

Private Sub Form_KeyPress(KeyAscii As Integer)

Print Chr(KeyAscii)

End Sub

The program will display the correponding character on the form as the user press a key on the keyboard that sends the ASCII code to the computer and the VB program convert that code into a character and display it on the form, it is that simple!

More to come.

No comments: