How to register a global hotkey #16

Global hotkeys are very handy when your application should be accessible at all time. For instance, you are browsing the Web and suddenly want to e-mail your friend. The most convenient way to do so would be to press a shortcut and the e-mail client would popup with a "new message" window no matter what program you are using at the time. This can be done with global shortcuts. They are system-wide no matter if the program you are using uses the same shortcut. A global shortcut is more important.

Here's the code to register and unregister a global shortcut.

uses
  Messages, Windows;
...
  // Your main form's class
  protected
    procedure WMHotKey(var Message: TMessage); message WM_HOTKEY;
...
implementation
...
procedure TMainForm.WMHotKey(var Message: TMessage);
begin
  // This example brings the application up front. Put your code here
  Application.BringToFront;
end;

Example of use:

RegisterHotKey(Handle, 100000 { Any unused number}, MOD_CONTROL, VK_F7);
// When closing program
UnregisterHotKey(Handle, 100000);
Author: Unknown
Added: 2007/06/02
Last updated: 2007/06/02