KeyPreview Property (AForm)

Returns or sets whether keyboard events for an object are invoked before keyboard events for controls on the form.

Syntax

Object.KeyPreview [ = bKeyPreview ]

The KeyPreview property syntax has the following parts:

 

Name

Type

Description

Object

AForm

AForm object

bKeyPreview

Bool

A boolean expression that specifies whether keyboard events for forms are invoked before keyboard events for controls.

Where

bKeyPreview can be one of the following:

 

Value

Description

True

The form receives keyboard events first and then the active control.

False *

The active control receives keyboard events and the form does not.

Comments

The keyboard events are OnKeyDown, OnKeyUp, and OnKeyPress.

This property can be used to create a keyboard-handling procedure for a form. For example, when an application uses function keys, it is best 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 OnKeyPress event, and set KeyCode to 0 in the form's OnKeyDown event.

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

Example

The following statement changes the AForm so the form receives keyboard events first and then the active control:

frm1.KeyPreview=True

Applies to

AForm

See Also

OnKeyDown, OnKeyPress, OnKeyUp