views:

88

answers:

2

I have a unit with a lot of actions, some actions have shortcuts and some have even the same shortcut. This is because they belong to different modules in applications and they are not in context at the same time.

Now I have a global nonmodal dialog (called objectinspector) that can be started anytime anywhere. It have some actions with shortcuts that are the same as other places in the modules.

So I want to temporary override those actions that share equal shortcuts when objectinspector is active and release it when deactivated.

I have this code in the objectinspectors OnActivate event:

  ClientActions.RegisterAction(ClientActions.actObjectInspectorNewWindow, ShowNewObject);
  • ClientActions is the unit that contains the actions.
  • RegisterAction is a method that connect the event with the action.
  • actObjectInspectorNewWindow is the actual action.
  • ShowNewObject is the event that should be called when action is executed.

So for every module and modalless dialog there is a couple of actions that is registred by the above method. The problem come when 2 actions share the same shortcut. It seems that the actions that first is registred win and is executed when user press the shortcut. I prefered that the last action that register an action with that shortcut is executed.

How can this be done ?

Regards

+1  A: 

If I understand your question correctly, you have more than one TActionList in your application. Actions in different action lists may use the same shortcut key. You want to control which action is executed when a

The way I'd do that would be to assign the form's OnShortCut event handler. In that event handler, call TActionList.IsShortCut() in the order of precedence that you want your action lists to have. When TActionList.IsShortCut() returns True, set the Handled parameter of OnShortCut to True and exit. When TActionList.IsShortCut() returns True, it will have actually executed the action.

Jan Goyvaerts
Thanks, I didn't know there was a shortcuthandler. But in my case I have one big global TActionList in unit ClientActions. So I assume this don't work in this case.
Roland Bengtsson
+1  A: 

Hi, if the actions are in the same TActionList, you must assign scNone to the action you want to disable and then assign the shortcut to the new action. Ex:

acEditCopy.Enabled:=False;
acEditCopy.ShortCut:=scNone;

acShowCalculator.Enabled:=True;
acShowCalculator.ShortCut:=ShortCut(Ord('C'), [ssCtrl])

This way, the code that checks if the key presed correspond to a shortcut in the ActionList doesn't break when it find the first Shourcut assigned. I hope this can help you, this thing drove me crazy about a month ago. :-)

José Romero
Hm, but in this case it may be hard to find out the action to disable.
Roland Bengtsson
You could write methods like:EnableDiableShortcutsModule1(AEnabled : Boolean)EnableDiableShortcutsModule2(AEnabled : Boolean)EnableDiableShortcutsModuleX(AEnabled : Boolean)And then call those methods depending the module you're about to enter/load in the aplication. Passing AEnabled = true wil assign the shortcuts to de desired actions for the module and clean all similar shortcut actions on the others and vice versa
José Romero