views:

362

answers:

3

I am using Delpho 2006. The Scenario:

On the data module I have an ActionList. One of the actions has a shortcut Ctrl+F4 and I want to have a secondary shortcut Ctrl+W. I tried all of the following:

Adding Ctrl+W to the actions SecondaryShortcut list in the IDE.

Adding it in the DataModuleCreate procedure using either

ActFileCloseFile.SecondaryShortCuts.Add('Ctrl+W');

or

ActFileCloseFile.SecondaryShortCuts.AddObject('Ctrl+W',
  TObject(Menus.ShortCut(87, [ssCtrl])));

Using both of these methods in the Create or FormShow procedure of the form where it will be used.

The primary shortcut always works, but not the secondary.

When I put the ActionList on the main form instead of the data module, it works by just adding Ctrl+W in the IDE. What do I do wrong?

+1  A: 

Not a real solution, but if you create the datamodule from within the mainform it works:

procedure TMainForm.FormCreate(Sender: TObject);
begin
  FDataModule := TMyDataModule.Create(self);
  TMyButton.Action := FDataModule.TheAction;
end;


procedure TMyDataModule.DataModuleCreate(Sender: TObject);
begin
  TheAction.SecondaryShortCuts.Add('Ctrl+W');
end;

I think the shortcuts are handled by the form that has the current focus. So yo will probably get the same problems if you are using them in another form.

Gamecat
Interestingly enough the primary ShortCut is handled even if only on the data module without your trick.
Ralph Rickenbach
+1  A: 

The most elegant solution found so far is this:

On the form you want to handle the SecondaryShortCut, add this to the OnShortCut event:

procedure TMyForm.FormShortCut(var Msg: TWMKey; var Handled: Boolean);
begin
  Handled := dmDataModule.ActionList1.IsShortCut(Msg);
end;

Alternative:

(This is not a real solution, but a workaround.)

Put an action list on the form that has an identical action to the one on the data module. In its execute and update events it only forwards the events to the data module action. The menus on the form use the local action.

In this case it suffices to add Ctrl+W to the SecondaryShortCuts property using the IDE.

Obviously, when the action on the data module changes, I have to change all local actions too.

Ralph Rickenbach
A: 
In this case I do not have a ActionList on the form and the data module ActionList has a state asNormal. And still it does not catch the SecondaryShortCut, only the primary ShortCut.
Ralph Rickenbach