Jessica Brown
Jessica Brown

Reputation: 8312

Adding a hot-key to my Delphi app

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.

Upvotes: 7

Views: 18841

Answers (3)

Laura
Laura

Reputation: 1

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.

Upvotes: 0

Logvinov Vladimir
Logvinov Vladimir

Reputation: 94

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;

Upvotes: 5

Ondrej Kelle
Ondrej Kelle

Reputation: 37221

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.)

Upvotes: 8

Related Questions