PRB: Form KeyDown Displays Inconsistent Behavior

Last reviewed: June 21, 1995
Article ID: Q113328
The information in this article applies to:

- Microsoft Visual Basic programming system for Windows, version 3.0

SYMPTOMS

The KeyDown event on a Form that has KeyPreview set to True may not fire when certain controls have the focus and arrow keys are pressed. Also, the ENTER key won't show up in the KeyPreview if the focus is on a command button.

CAUSE

When any of the following controls have the Focus, pressing the arrow keys will not cause the Form_KeyDown event to fire:

  • Option Button
  • Command Button
  • Check Box
  • 3D Check Box
  • 3D Option Button
  • 3D GroupPushButton

This is not the case when controls that accept Text input, such as a text box, have the focus. In the case of a control that accepts text input, you'll find that the Form_KeyDown event fires normally.

STATUS

This behavior is by design. The problem is that these controls are subclassed off a default control type provided by Windows. For more information, please see the Visual Basic version 3.0 "Professional Features Book 1" manual, "Control Development Guide," page 87, the paragraph that begins, "If Click were implemented ..."

The superclass in Windows treats these keys in a special way, so that the control always gets the keystroke before the form does. When the control itself handles the event, it can reflect the keystroke back to the form for the KeyPreview. But if the superclass handles the event, the control must also avoid handling it in order to prevent conflicts, so there is no way to reflect the keystroke back to the form.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Set the KeyPreview property of the form to True.

  3. Add two command buttons (Command1 and Command2) to the form.

  4. Add the following code to the Form_KeyDown procedure:

       Sub Form_KeyDown()
          Print "Hello from keydown"
       End Sub
    
    

  5. Run the program. Pressing any of the arrow keys does not evoke the Form_KeyDown event. Instead, the program cycles through the controls on the form, making each in turn become the active control.

  6. Add a Text box (Text1) to the form.

  7. Run the program.

Pressing the arrow keys will evoke the Form_KeyDown when focus shifts to the Text box.


Additional reference words: 3.00
KBCategory: kbprg kbcode kbprb
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.