expression.KeyPreview
expression Required. An expression that returns one of the objects in the Applies To list.
Remarks
The KeyPreview property uses the following settings.
Setting | Visual Basic | Description |
---|---|---|
Yes | True | The form receives keyboard events first, then the active control receives keyboard events. |
No | False | (Default) Only the active control receives keyboard events. |
Note You can set the KeyPreview property by using the form's property sheet, a macro, or Visual Basic.
You can set the KeyPreview property in any view.
You can use the KeyPreview property to create a keyboard-handling procedure for a form. For example, when an application uses function keys, setting the KeyPreview property to True allows you to process keystrokes at the form level rather than writing code for each control that might receive keystroke events.
To handle keyboard events only at the form level and prevent controls from receiving keyboard events, set the KeyAscii argument to 0 in the form's KeyPress event procedure, and set the KeyCode argument to 0 in the form's KeyDown and KeyUp event procedures.
If a form has no visible or enabled controls, it automatically receives all keyboard events.
Example
In the following example, the KeyPreview property is set to True 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