Title: Action Lists in Data Modules
Question: How can I put action lists in a datamodule and the actions still catch all keyboard shortucts evoqued in one form ?
Answer:
When designing an application, I try (and I think everyone tries) to make forms
more clean as possible.
To do that I normally use datamodules, where I put all database related
components, image lists and other non-visible components.
I had one problem though, the actions of an action list (in a datamodule)
couldn't catch the shorcuts I sent to a form.
The only way to make the actions work, was attaching them to a TMenuItem, or
some other component which recognises shortcuts in its own right.
Anyway there's a solution. TActionList contains a method called IsShortCut.
Delphi's help tells us that it is used internally, and that normally we would
not have to call it directly. What this method does, is traverse the actions
contained within the list, and executes the first one it finds with the matching
shortcut key.
So, what we need to do is call this IsShortCut method when the user hits a key
from the main form. As it turns out, Delphi 5 gives us a very easy way to do
this: The OnShortCut event.
Thus:
procedure TForm1.FormShortCut(var Msg: TWMKey; var Handled: Boolean);
begin
Handled := DataModule1.ActionList1.IsShortCut(Msg);
end;
Now any shortcut keys within ActionList1 automatically work from Form1.