OnKeyPreview

Syntax

Sub OnKeyPreview(Sender: Object; Args: IKeyPreviewEventArgs);

Begin

//set of operators;

End Sub OnKeyPreview;

Parameters

Sender. Parameter that returns the component that has generated the event.

Args. Parameter that enables the user to determine event parameters.

Description

The OnKeyPreview event occurs prior to each event related to key pressing.

Comments

This event occurs before such events as OnKeyDown, OnKeyPress and OnKeyUp. It can be used to cancel the following event or to reassign the key that generated this event.

NOTE. Take into account the following facts reassigning the keys:

  - Alphanumeric keys (and their combinations with control ones) can be reassigned only for alphanumeric keys (combinations with control ones).

  - Functional and control keys (or their combinations) can be reassigned only for functional and control keys (combinations of functional and control ones).

The OnKeyPreview event occurs for the whole component hierarchy from the currently focused component to the form itself. If the form is displayed in the frame, the OnKeyPreview event via the Frame component is moved to the highest level to the parent form, which contains the Frame component.

Example

Executing the example requires a form. Several components that can receive focus are placed on the form. The following procedure is used as a handler of the OnKeyPreview event for all components:

Sub ProcedureOnKeyPreview(Sender: Object; Args: IKeyPreviewEventArgs);
Begin
    If Args.Key = Keys.A Then
        Args.Key := Keys.B;
    End If;
End Sub ProcedureOnKeyPreview;

After executing the example, if A Latin capital letter was pressed in the area of any component, when the text was entered, B letter is passed in the argument in the following events (such as OnKeyDown, OnKeyPress, OnKeyUp).

See also:

IControl