Microsoft KB Archive/113328

= PRB: Form KeyDown Displays Inconsistent Behavior =

Article ID: 113328

Article Last Modified on 1/9/2003

-

APPLIES TO


 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q113328





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.



Steps to Reproduce Behavior
 Start a new project in Visual Basic. Form1 is created by default. Set the KeyPreview property of the form to True. Add two command buttons (Command1 and Command2) to the form.  Add the following code to the Form_KeyDown procedure: Sub Form_KeyDown Print "Hello from keydown" End Sub  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.</li> Add a Text box (Text1) to the form.</li> Run the program.</li></ol>

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

Additional query words: 3.00

Keywords: kbprb KB113328

-

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

© Microsoft Corporation. All rights reserved.