Non-Menu Keys Disabled When Menu Pulled Down (99811)



The information in this article applies to:

  • Microsoft Visual Basic Standard Edition for Windows 2.0
  • Microsoft Visual Basic Standard Edition for Windows 3.0
  • Microsoft Visual Basic Professional Edition for Windows 2.0
  • Microsoft Visual Basic Professional Edition for Windows 3.0
  • Microsoft Visual Basic Standard Edition for Windows 1.0

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.

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.

Modification Type:MajorLast Reviewed:12/12/2003
Keywords:KB99811