OnKeyPreview

Syntax

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

Begin

//set of operators;

End Sub OnKeyPreview;

Parameters

Sender. Parameter that returns the component, generated an event

Args. Parameter that allows to determine the event parameters.

Description

The OnKeyPreview event occurs prior to each event requiring 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).

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