The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for
Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0
SUMMARY
Although there is no ON KEY GOSUB statement in Visual Basic, you can
achieve an effect identical to ON KEY event handling. Visual Basic forms
and controls that are able to get focus have a KeyDown event procedure that
can simulate the effects of the ON KEY statements in Basic interpreters and
compilers for MS-DOS. In fact, the KeyDown event procedure is more powerful
and flexible than the ON KEY statement.
MORE INFORMATION
Pressing a key while a Visual Basic form or control has the focus executes
that form or control KeyDown event procedure. Within the KeyDown event
procedure, you can call a global procedure and pass the actual key states
to the global procedure. You can use this to create an effect in Visual
Basic for Windows that is identical to the effect caused by trapping ON KEY
events in Basic interpreters and compilers for MS-DOS. In Visual Basic, you
can also pass the name of the control or form where the KeyDown event
occurred, so the global procedure will know which control or form
called it.
Here's a small example:
- In the Visual Basic Project window, double-click a form or module
(GLOBAL.BAS in Visual Basic version 1.0) to bring up the code window.
Move to the general-declaration section of the form or module. Then
from the Visual Basic Code menu, choose Load text, and load the
CONSTANTS.TXT file that came with Visual Basic.
Note: in Visual Basic version 1.0, if you already have text in the
GLOBAL.BAS file, create a new module, add the CONSTANTS.TXT file to the
new module, and then cut and paste the text into the GLOBAL.BAS file.
- Add two text boxes (Text1 and Text2) to a form.
- In the Text1_KeyDown event procedure, add the following code:
Call OnKeyGoSub(KeyCode, Shift, Text1)
- In the Text2_KeyDown event procedure, add the following code:
Call OnKeyGoSub(KeyCode, Shift, Text2)
- Add a Label (Label1) to to the form.
- In the general-declaration section for the form, add this 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
- 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.
This example is limited, but shows you how to simulate the ON KEY
statements or key trapping in Visual Basic by placing the call to the key
trap procedure in any KeyDown event procedure.
|