KeyPreview

Returns or sets a value that determines whether keyboard events for forms are invoked before keyboard events for controls. The keyboard events are KeyDown, KeyUp, and KeyPress.

Syntax

object.KeyPreview [= boolean]

The parts of the KeyPreview property syntax are described in the following table.

Part Description
object An object expression that evaluates to an object.
boolean A Boolean expression that specifies how events are received, as described in Settings.

Settings

The settings for boolean are described in the following table.

Setting Description
True The form receives keyboard events first and then the active control.
False (Default) The active control receives keyboard events; the form does not.

Remarks

You can use this property to create a keyboard-handling procedure for a form. For example, when an application uses function keys, you'll want to process the keystrokes at the form level rather than writing code for each control that might receive keystroke events.

If a form has no visible and enabled controls, it automatically receives all keyboard events.

To handle keyboard events only at the form level and not allow controls to receive keyboard events, set KeyAscii to 0 in the form's KeyPress event, and set KeyCode to 0 in the form's KeyDown event.

Note Some controls intercept keyboard events so that the form cannot receive them. Examples include the ENTER key when focus is on a CommandButton control and arrow keys when focus is on a ListBox control.