Applies To Form.
Description
You can use the KeyPreview property to specify whether the form-level keyboard event procedures are invoked before a control's keyboard event procedures.
Note The keyboard events are KeyDown, KeyUp, and KeyPress.Setting
The KeyPreview property uses the following settings.
Setting | Description | Visual Basic |
Yes | The form receives keyboard events first, then the active control receives keyboard events. | True (–1) |
No | (Default) Only the active control receives keyboard events. | False (0) |
See Also KeyDown, KeyUp events, KeyPress event.
Example In the following example, the KeyPreview property is set to True (–1) in the form's Load event procedure. This causes the form to receive keyboard events before they are received by any control. The form KeyDown event then checks the KeyCode argument value to determine if the F2, F3, or F4 keys were pushed.Private Sub Form_Load()
Me.KeyPreview = True
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyF2
' Process F2 key events.
Case vbKeyF3
' Process F3 key events.
Case vbKeyF4
' Process F4 key events.
Case Else
End Select
End Sub