Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I set global hot keys and answer to them?

How To

How do I set global hot keys and answer to them?

by  Glenn9999  Posted    (Edited  )
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.

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top