Adding a hot-key to my Delphi app
Asked Answered
P

3

7

Say I have a form, with a menu bar on it. I have an item on the menu bar, a TMenuItem, for which I can assign a shortcut key combo, say, for example Ctrl+I. But when I assign the ShortCut property for the TMenuItem, it seems to just change the visual appearance of the menu item to show the shortcut code rather than automatically listening for the short-cut key to be pressed and triggering my ActionManager code.

My google-fu seems to be failing today, I'm only finding articles about how to assign global-hot-keys for windows, not how to assign application-specific hot-keys that only work on the active form.

Can anyone outline for me the steps necessary to add a hot-key beyond just adding the shortcut property in the menu. I'm thinking somewhere I probably need to set the form to be listening for keyboard input and trap the keypress and respond to it? But I'm not exactly sure where or what the Delphi way to do that would be.

Poliard answered 24/7, 2012 at 5:39 Comment(4)
Normally, there is no problem in doing what you wish. It just works, automatically. Since you are using an action manager, you should use the ShortCut property of the TAction, not the TMenuItem.Hogg
Yes, just assign ShortCut property and it's all good.Concordat
Think of the Menu Items as just "one dynamically built way of presenting my statically defined actions", therefore, I set up my actions, and not my menu item properties. The menu item properties originate at the Action list, or action manager, and propagate outwards.Lissner
@AndreasRejbrand yes, by action manager works, thanks =) ... but there is a way to allow the own user define what ShortCut wants, something like customize the ShortCuts?Show
B
8

You seem to be using Actions (ActionManager), so assign your shortcut to the relevant Action instead. (Assigning the Action to the MenuItem will then assign the shortcut to the menu item, too.)

Benevolence answered 24/7, 2012 at 7:23 Comment(0)
P
5

Consider the example of window handles are not provided for VCL message, for which we use WM_HOTKEY. This message is sent by registered window in Windows hotkey that allows the program to respond to it, even without the input focus:

type
TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    // Declare a event handler
     procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
   // Registering a hotkey Ctrl+Alt+F5
   RegisterHotKey(Handle, 0, MOD_CONTROL or MOD_ALT, VK_F5);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  // Unregisters a hotkey
   UnRegisterHotKey(Handle, 0);
end;

procedure TForm1.WMHotKey(var Msg: TWMHotKey);
begin
   // This procedure is called when a window message WM_HOTKEY
   inherited;  // We give the form to process the message,
               // if she already has its handler
   Beep;       // We perform additional actions
end;
Pawpaw answered 24/7, 2012 at 17:13 Comment(3)
-1 question clearly states that system wide hot keys are not what is requiredConcordat
anyhow, its a useful solution that shows how to set up global hot keyBomb
Perfect, just what I needed!Tsimshian
S
0

Great to know about that way of WM_HOTKEY, but it is too extreme:

  • It acts at system level, so if another app is active and on such app you press the hotkey, it is also captured by our app.
  • It does noy only work when our app is with keyboard focus

The good thing is that it can use hotkeys like "Control" & "+" (VK_ADD = 107) and "Control" & "-" (VK_SUBTRACT = 109).

Buy what i would want is the HotKey only if our Application is active and not affecting any other APP hoytkeys.

Thanks, at least i have a starting point.

Sharell answered 15/12, 2022 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.