PRB: Form KeyDown Displays Inconsistent Behavior
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
- 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.
- Add a Text box (Text1) to the form.
- Run the program.
Pressing the arrow keys will evoke the Form_KeyDown when focus shifts
to the Text box.
Additional query words:
3.00
Keywords :
Version :
Platform :
Issue type :