PRB: Arrow Keys Do Not Always Produce a KeyDown EventID: Q138530
|
The KeyDown event for a form is not invoked when using the arrow
(navigational) keys, even though the Keypreview property is set to True for
the form. Instead the arrow keys cause other controls to gain the focus,
even if the TabStop property is set to False for those controls.
NOTE: The DEL key may not trigger the KeyDown event either. For instance,
in Visual Basic 4.0 the DEL key in a masked edit box does not trigger the
KeyDown event.
You can get around this behavior by placing the problem controls onto a container that has a trappable KeyDown event. This would include the TabStrip, SSTab, PictureBox, and so forth. The following example uses a PictureBox because it is easily made unnoticeable.
TabStop False
BorderStyle 0 - None
BackColor Same as From (by default)
Private Sub Command5_Click()
'some code here
Picture1.SetFocus
End Sub
Private Sub Command6_Click()
'some code here
Picture1.SetFocus
End Sub
Private Sub Command7_Click()
'some code here
Picture1.SetFocus
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Debug.Print "Form_KeyDown - " & KeyCode
End Sub
Private Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer)
Debug.Print "Picture1_KeyDown - " & KeyCode
End Sub
Microsoft is researching this behavior and will post new information here in the Microsoft Knowledge Base as it becomes available.
Whether or not the form receives a KeyDown event depends on the type of control that has the focus when the arrow key is pressed. The KeyDown event of the form will be correctly activated if the arrow keys are used when a control such as a text box, a list box, or a picture box has the focus. The KeyDown event for the form is not activated when Command buttons, check boxes, and option buttons have the focus when an arrow key is pressed. In all cases, this behavior does not depend on whether Tabstop is set to True or False for the control.
Additional query words:
Keywords : kbVBp300 kbVBp400 kbGrpVB kbDSupport
Version : WINDOWS:3.0,4.0
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: July 27, 1999