7

I want to have a shortcut key combination (like Ctrl+Alt+D) in my app to invoke a function, but I don't want the shortcut to appear on any menu. Is it possible to have a shortcut available in your app that is otherwise invisible?

menjaraz
  • 7,551
  • 4
  • 41
  • 81
rossmcm
  • 5,493
  • 10
  • 55
  • 118

2 Answers2

12

you can use the OnShortCut event of the TApplicationEvents component to this task

check this code

procedure TForm1.ApplicationEvents1ShortCut(var Msg: TWMKey;
  var Handled: Boolean);
begin
  if (Msg.CharCode = Ord('D')) and (HiWord(Msg.KeyData) and KF_ALTDOWN <> 0) and  (GetKeyState(VK_CONTROL) < 0) then
  begin
    ShowMessage('Ctrl+Alt+D Pressed') ;
    Handled := true;
  end;
end;
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • Excellent. Works a treat and doesn't involve any action components. – rossmcm Jul 25 '10 at 21:57
  • 3
    If you are already using a TActionList for the other actions on the form, just adding another TAction with the required shortcut is more elegant and improves readability. (as suggested by Giacomo Degli Esposti) – dummzeuch Jul 26 '10 at 09:45
  • I'm not using actionlists although I probably should, It's an app that started life before I knew what they were and I've never been bothered enough to retro-fit them. – rossmcm Jul 26 '10 at 13:38
8

Yes it's possible. You must add an Object of class TAction to your form. You can specify a keyboard shortcut for the Taction and then put your code in event OnExecute of the TAction.

Note that you cannot add a Taction directly to your form, you must put a TactionList on your form and then you can add a Taction to your TActionList.

Giacomo Degli Esposti
  • 2,392
  • 1
  • 15
  • 20