Non-Menu Keys Disabled When Menu Pulled Down

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

When a Visual Basic menu is pulled down, all non-menu keystrokes are disabled and keystrokes cannot be detected. This behavior is by design. When a menu is down, all keystrokes just beep or do nothing, except for the keystrokes that control the menu.

You cannot determine which menu item the user chose, until after the user clicks the menu item or presses ENTER. The Click event for the menu item will then give you the chosen menu item.

MORE INFORMATION

Access Keys Give a User Keyboard Access to Menu Items

To give a user keyboard access to a menu item, insert an ampersand (&) immediately in front of a letter in the Caption by using the Menu Design Window. At run time, this letter (called the access key) is underlined. The user can change the focus to a menu or command by pressing ALT plus the letter (access key).

You can use an access key such as ALT+F to give focus quickly to a menu, command, or control by using the keyboard as an alternative to the mouse.

Unlike shortcut keys (such as F10 or CTRL+T, which are also assigned in the Menu Design Window), access keys do not execute commands when pressed, until the ENTER key is pressed. If you open a menu with an access key, then all non-menu keystrokes are disabled until you press a menu-control key such as ENTER, ESC, or ALT.

Step-by-Step Example

To trap all keystrokes in the form instead of the form's controls, you can set the form's KeyPreview property to True. However, because menu controls disable non-menu key presses when the menu is down, you won't be able to preview or trap keys that are pressed when the menu is down, as this example demonstrates:

  1. Set the KeyPreview property to True for Form1.

  2. Using the Menu Design window, add a Main menu with two submenus:

    Mainmenu

          Submenu1
          Submenu2
    

  3. In the KeyPress event for the form, print trapped key values as follows:

       Sub Form_KeyPress (KeyAscii As Integer)
          Print Str$(KeyAscii)
       End Sub
    
    

  4. Run the program. Press any alphanumeric key, and its ASCII value will be trapped by Form_KeyPress and be printed.

  5. Click the Mainmenu menu to drop it down. While the menu is down, non-menu key strokes cause the computer to either beep or do nothing. For example, pressing the x key causes the computer to beep; pressing the F1 key causes the computer to do nothing; but pressing the s key toggles the menu selection between Submenu1 and Submenu2 because both begin with the letter s.

  6. Press ESC, ALT, ENTER, or click the menu to make the menu go away and reenable trapping of keystrokes.

While the menu is down, the Form_KeyPress event detects no keys.


Additional reference words: 1.00 2.00 3.00
KBCategory: kbprg kbcode
KBSubcategory: PrgCtrlsStd


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.