Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 895
Jessica Brown Avatar asked Jul 24 '12 05:07

Jessica Brown


People also ask

How do you add hotkeys?

To assign a keyboard shortcut do the following: Begin keyboard shortcuts with CTRL or a function key. Press the TAB key repeatedly until the cursor is in the Press new shortcut key box. Press the combination of keys that you want to assign.

What is HotKey app?

With HotKey You can define keyboard shortcuts to launch Applications or to open folders in Finder. Another way is to open the Apps by selecting them from the Status-Menu in Your Mac's menu bar. Another Feature: Show the content of the clipboard with one keyboard shortcut.

What key do you press to add a new line of text?

Thankfully, there is a keyboard shortcut that moves to the next line. Move the text cursor to where you want the new line to begin, press the Enter key, hold down the Shift key, and then press Enter again.


2 Answers

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

like image 92
Ondrej Kelle Avatar answered Oct 30 '22 16:10

Ondrej Kelle


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;
like image 23
Logvinov Vladimir Avatar answered Oct 30 '22 16:10

Logvinov Vladimir