Table of Contents

TatEventAdapters.DefineEventAdapter Method

Registers a new event adapter for the event type specified by ARttiInfo.

Remarks

Once an adapter is registered for an event type such as TNotifyEvent, the scripter can handle all Delphi events of that type on any object (OnClick, OnChange, OnEnter, etc.).

Syntax

Unit: atScript

function TatEventAdapters.DefineEventAdapter(ARttiInfo: PTypeInfo; ADispatcherClass: TDispatcherClass; AMethodCode: Pointer; ACheck: Boolean): TatEventAdapter;

Parameters

<-> Parameter Type Description
ARttiInfo PTypeInfo RTTI type information for the event type being registered (e.g., TypeInfo(TNotifyEvent)).
ADispatcherClass TDispatcherClass The TatEventDispatcher descendant class to instantiate when an event of this type is set. The dispatcher handles the event by calling the assigned script routine with the correct parameters.
AMethodCode Pointer The address of the method within the dispatcher class that will be called when the event fires.
ACheck Boolean When True, checks if an adapter already exists for this event type and updates it if so. When False, always creates a new adapter. Returns the created or updated TatEventAdapter object.

Examples

To define an event adapter for the TKeyEvent. The TKeyEvent is declared in the VCL as following.

TKeyEvent = procedure( Sender: TObject; var Key: Word;  Shift: TShiftState) of object;

So you need to register a new adapter using the following call to DefineEventAdapter.

DefineEventAdapter(TypeInfo(TKeyEvent), TatMyKeyEventDispatcher, @TatMyKeyEventDispatcher.__TKeyEvent);

In the first parameter you pass the type info of the TKeyEvent type. In second parameter, the dispatcher class to be instantiated that will handle the event and dispatch it to scripter. In third parameter, the address of the method in dispatcher class that will be called when the event fires. You can then implement your dispatcher this way.

TatMyKeyEventDispatcher = class(TatEventDispatcher) private procedure __TKeyEvent( Sender: TObject; var Key: Word;  Shift: TShiftState); end; procedure TatMyKeyEventDispatcher.__TKeyEvent( Sender: TObject; var Key: Word;  Shift: TShiftState); var KeyTemp: variant; ShiftTempSet: TShiftState; ShiftTemp: variant; begin if DoOnExecuteEvent then begin //Call event previous set if AssignedMethod(BeforeCall) then TKeyEvent(BeforeCall)(Sender,Key,Shift); //Put Key in a variant type so that script routine can update its value KeyTemp := Integer(Key); //Converts TShiftState to integer so it can be read from script ShiftTempSet := Shift; ShiftTemp := IntFromSet(ShiftTempSet, SizeOf(ShiftTempSet)); //Execute the script routine that will handle the event, passing the event parameters to it if Assigned(Scripter) and (RoutineName > '') then Scripter.ExecuteSubroutine(RoutineName, [Sender,KeyTemp,ShiftTemp]); //Update the Key parameter received from the script Key := VarToInteger(KeyTemp); //Call event previous set if AssignedMethod(AfterCall) then TKeyEvent(AfterCall)(Sender,Key,Shift); end;

See also