Delphi - grab all action.onexecute from application

I have a great application with hundreds of TActions. Each of them is used and implements various functionalities.

Is it possible to catch (hook) all TAction.OnExecute from the application? Is there any Windows message that I can connect so that I can register the name of the action that was performed?

+6
source share
2 answers

You just need to add the TApplicationEvents object and handle the OnActionExecute event. The event handler is passed by the Action instance and therefore can easily get the name of the action.

The OnActionExecute event fires before the OnExecute event OnExecute . You can even stop the OnExecute event from triggering by setting the Handled parameter to True in the OnActionExecute event OnActionExecute .

+12
source

Based on David's answer, I made a small example:

 program Project1; uses ExceptionLog, Forms, Unit2 in 'Unit2.pas' {Form2}, AppEvnts, Classes, Windows, SysUtils; {$R *.res} type TAppEventsHack = class procedure onAppEvtExec(Action:TBasicAction;var Handled:Boolean); end; var aEvHack : TAppEventsHack; aAppEvents : TApplicationEvents; { TAppEventsHack } procedure TAppEventsHack.onAppEvtExec(Action: TBasicAction; var Handled: Boolean); begin OutputDebugString(PAnsiChar(Action.Name)); Handled := False; end; begin Application.Initialize; try aEvHack := TAppEventsHack.Create; aAppEvents := TApplicationEvents.Create(nil); aAppEvents.OnActionExecute := aEvHack.onAppEvtExec; Application.CreateForm(TForm2, Form2); Application.Run; finally freeandnil(aEvHack); freeandnil(aAppEvents); end; end. 
+2
source

Source: https://habr.com/ru/post/904231/


All Articles