Simulating ON KEY Key Trapping with KeyDown Event in VB

ID Number: Q75858

1.00

WINDOWS

Summary:

Although there is no ON KEY GOSUB statement in Visual Basic, the

effect of ON KEY event handling can be achieved in Visual Basic.

Visual Basic forms and controls that are able to get focus within

Visual Basic have a [form/control]_KeyDown event procedure that can

simulate the effects of the ON KEY statements of Basic interpreters

and compilers for DOS. The process of using the KeyDown is more

powerful and more flexible than the ON KEY statement.

This information applies to the Microsoft Visual Basic programming

system version 1.0 for Windows.

More Information:

If you press a key while one of your Visual Basic forms or controls

has the focus, the KeyDown event procedure for that form or control

will be executed. Within the KeyDown event procedure, you can call a

global procedure and pass the actual key states to the global

procedure to create the same effect as the ON KEY event trapping that

is performed in Basic interpreters and compilers for DOS.

You can also pass the control or form where the KeyDown occurred to

the global procedure. Passing the control or form itself will allow

the global procedure to tell what control or form called the global

procedure.

To create a small example, do the following:

1. Within Visual Basic's Project window, double-click the GLOBAL.BAS

file to bring up the code window. From Visual Basic's Code menu,

choose "Load text...". Load the CONSTANTS.TXT file that came with

Visual Basic. Note: If you already have text within the GLOBAL.BAS

file, you will need to create another module, add the CONSTANTS.TXT

to this file, then cut and paste to the GLOBAL.BAS file.

2. Create two text boxes on a form.

3. In the Text1_KeyDown event procedure, add the following code:

Call OnKeyGoSub(KeyCode, Shift, Text1)

4. In the Text2_KeyDown event procedure, add the following code:

Call OnKeyGoSub(KeyCode, Shift, Text2)

5. Add a Label to Form1 called Label1.

6. In the general-declaration section for Form1, add the following

procedure:

Sub OnKeyGoSub (KeyCode%, Shift%, Ctrl As Control)

Select Case KeyCode%

Case KEY_MENU: Key$ = ""

Case KEY_SHIFT: Key$ = ""

Case KEY_CONTROL: Key$ = ""

Case KEY_F1: Key$ = " F1 "

Case KEY_UP: Key$ = " UP key"

Case KEY_CAPITAL: Key$ = "CAP LOCKS"

Case Else: Key$ = Chr$(KeyCode%)

End Select

Select Case Shift%

Case SHIFT_MASK: Shft$ = "Shift"

Case ALT_MASK: Shft$ = "Alt"

Case CTRL_MASK: Shft$ = "Ctrl"

Case Else: Shft$ = ""

End Select

Label1.Caption="Key:"+ Shft$+ " "+ Key$

End Sub

7. Run the program. Move back and forth between the two text boxes

using either the TAB key or the mouse. Experiment with any key in

combination with the ALT, CTRL, and SHIFT keys. Also try the F1

and UP ARROW keys.

The above example is limited, but shows how you can simulate the ON

KEY statements or key trapping within Visual Basic. Placing the call

to the key trap procedure within any KeyDown event procedure will

simulate the ON KEY statement.

Additional reference words: 1.00