Tuesday, January 4, 2011

How to register a global hotkey

How to register a global hotkey

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);
 
 
Source : http://www.delphidabbler.com/tips/16 

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum