Capturing application messages
Copyright © 2000 Ernesto De Spirito
![]() |
Sometimes we need to capture Windows messages at an application level. One
way is by a hook, and another way is by using the Message event of
the Application object which is triggered every time the application
gets a Windows message. We are going to follow the second path. There
are two ways to set an event handler for this event: one is using
an ApplicationEvents object that comes in Delphi 5 (simply
double click its OnMessage combo box in the Object Inspector) and
the other is doing it by hand:
In the private section of your main form add the following declaration:
procedure ApplicationMessage(var Msg: TMsg; var Handled: Boolean);Assign the
OnMessageproperty in theCreateevent of the form for example:procedure TForm1.FormCreate(Sender: TObject); begin Application.OnMessage := ApplicationMessage; end;When you want to stop capturing messages all you have to do is set
OnMessagetonil:procedure TForm1.FormDestroy(Sender: TObject); begin Application.OnMessage := nil; end;
Finally you should implement the procedure. For example, we are going to
trap the keyboard messages WM_KEYUP and WM_KEYDOWN to
convert the decimal point of the numeric keypad to a comma (this is useful in
Spanish applications).
procedure TForm1.ApplicationMessage(var Msg: TMsg;
var Handled: Boolean);
begin
case Msg.Message of
WM_KEYDOWN, WM_KEYUP:
case Msg.wParam of
// Replace the period in the numeric keypad (key code = 110)
// with a comma (key code = 188).
110: Msg.wParam := 188;
end;
end;
end;
Now you can drop an Edit control on your form and test it.
![]() |



