INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How To

How do I set global hot keys and answer to them? by Glenn9999
Posted: 14 Nov 15

One thing that may be of interest is to set a hot key where your program will respond to it anywhere on the system. In addition, configuring such hot keys will be discussed in this FAQ.

Translating HotKey Info to TShortCut
This code is useful to translate from TShortCut values used within Delphi to values needed for the hotkey registration API calls.

CODE

procedure HotKeyToShortCut(hotkey: Word; Modifier: UInt; var HKeyCtrl: TShortCut);
// convert HotKey value from shortcuts to Hotkey value compatible with TShortCut
var
  Shift: TShiftState;
begin
  Shift := [];
  if (Modifier and MOD_SHIFT) <> 0 then
    Shift := Shift + [ssShift];
  if (Modifier and MOD_CONTROL) <> 0 then
    Shift := Shift + [ssCtrl];
  if (Modifier and MOD_ALT) <> 0 then
    Shift := Shift + [ssAlt];
  HKeyCtrl := ShortCut(HotKey, Shift);
end;

procedure ShortCutToHotKey(HKeyCtrl: TShortCut; var Key: Word; var Modifer: UInt);
// convert THotkey compatible Hotkey to shortcut hotkey.
var
  Shift: TShiftState;
begin
  ShortCutToKey(HKeyCtrl, Key, Shift);
  Modifier := 0;
  if (ssShift in Shift) then
  Modifier := Modifier or MOD_SHIFT;
  if (ssAlt in Shift) then
  Modifier := Modifier or MOD_ALT;
  if (ssCtrl in Shift) then
  Modifier := Modifier or MOD_CONTROL;
end;

// example calls - translate to ShortCut
HotKeyToShortCut(keyComb, Modifier, HotKey1);

// translate ShortCut to HotKey
ShortCutToHotKey(HotKey1, keycomb, modifier);

// to show a ShortCut option in a caption
Label1.Caption := 'Press ' + ShortCutToText(t) + ' to trigger hotkey event.'; 

Register and Deregister Shortcuts
Once you have the hot key values set, you need to be able to register and deregister them with the system.

CODE

var
  keycomb: Word;  // key press.
  Modifier: UINT; // modifier such as any combination of Shift, Alt, Ctrl.
  ExecuteHotKey: Integer;  // key value or message.

// register
  ExecuteHotKey := GlobalAddAtom('ExecuteHotKey');
  RegisterHotKey(Handle, ExecuteHotKey, Modifier, keycomb);

// unregister
  UnRegisterHotKey(Handle, ExecuteHotKey);
  GlobalDeleteAtom(ExecuteHotKey); 

Responding to Your HotKey
Now that you have your hot key registered within the system, you'll want to be able to respond to it. To do that, you define a WM_HOTKEY event (which can be done via numerous methods). The one I'll show for this FAQ is as follows:

CODE

TForm1 = class(TForm)
...
  private
    procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;

procedure TForm1.WMHotKey(var Msg: TWMHotKey);
begin
  if (msg.HotKey = ExecuteHotKey) then
    begin
  //  do hotkey related things.
    end;
end; 

Of course, you can have multiple hot key actions within the same WMHotKey method.

Now, if your program needs it, you should be able to define a hot key for your program which will be answered anywhere on the system.


Back to Embarcadero: Delphi FAQ Index
Back to Embarcadero: Delphi Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close