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

Last reviewed: June 21, 1995
Article ID: Q75858
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:

  1. 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.

  2. Add two text boxes (Text1 and Text2) to 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 (Label1) to to the form.

  6. 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
    
    

  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.

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 reference words: 1.00 2.00 3.00
KBCategory: kbenv kbprg
KBSubcategory: EnvtRun


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.