Microsoft KB Archive/99811

= Non-Menu Keys Disabled When Menu Pulled Down =

Article ID: 99811

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 Q99811



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.



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:

 Set the KeyPreview property to True for Form1.  Using the Menu Design window, add a Main menu with two submenus:   Mainmenu Submenu1 Submenu2   In the KeyPress event for the form, print trapped key values as follows: Sub Form_KeyPress (KeyAscii As Integer) Print Str$(KeyAscii) End Sub  Run the program. Press any alphanumeric key, and its ASCII value will be trapped by Form_KeyPress and be printed. 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. Press ESC, ALT, ENTER, or click the menu to make the menu go away and reenable trapping of keystrokes.</li></ol>

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

Additional query words: 2.00 3.00

Keywords: KB99811

-

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

© Microsoft Corporation. All rights reserved.