Microsoft KB Archive/75858

= How to Simulate ON KEY & Key Trapping by Using KeyDown Event =

Article ID: 75858

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q75858



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$ = &quot;&quot; Case KEY_SHIFT: Key$ = &quot;&quot; Case KEY_CONTROL: Key$ = &quot;&quot; Case KEY_F1: Key$ = &quot; F1 &quot; Case KEY_UP: Key$ = &quot; UP key&quot; Case KEY_CAPITAL: Key$ = &quot;CAP LOCKS&quot; Case Else: Key$ = Chr$(KeyCode%) End Select Select Case Shift% Case SHIFT_MASK: Shft$ = &quot;Shift&quot; Case ALT_MASK: Shft$ = &quot;Alt&quot; Case CTRL_MASK: Shft$ = &quot;Ctrl&quot; Case Else: Shft$ = &quot;&quot; End Select Label1.Caption=&quot;Key:&quot;+ Shft$+ &quot; &quot;+ 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.</li></ol>

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.

Additional query words: 2.00 3.00

Keywords: KB75858

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.