whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1548)

Database (90)
Files (137)
Forms (107)
Graphic (116)
IDE (21)
Indy (5)
Internet / LAN (131)
IntraWeb (0)
Math (76)
Misc (127)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (243)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

22 Visitors Online


 
...use the THotKey component to register a global Hotkey?
Autor: Thomas Stutz
[ Print tip ]  

Tip Rating (35):  
     




uses
  
Windows, Messages, SysUtils,  Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Menus;

type
  
TForm1 = class(TForm)
    HotKey1: THotKey;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    
{ Private declarations }
    
Hotkey_id : Integer;
    procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
  public
    
{ Public declarations }
  
end;

var
  
Form1: TForm1;

implementation

{$R *.dfm}

// WM_HOTKEY Hotkey Handler
procedure TForm1.WMHotKey(var Msg: TWMHotKey);
begin
  if 
Msg.HotKey = Hotkey_id then
    
ShowMessage('Hotkey empfangen');
end;

// Transform TShortCut to use for RegisterHotKey
// TShortCut für RegisterHotKey umwandeln
procedure ShortCutToHotKey(HotKey: TShortCut; var Key : Word; var Modifiers: Uint);
var
  
Shift: TShiftState;
begin
  
ShortCutToKey(HotKey, Key, Shift);
  Modifiers := 0;
  if (ssShift in Shift) then
  
Modifiers := Modifiers or MOD_SHIFT;
  if (ssAlt in Shift) then
  
Modifiers := Modifiers or MOD_ALT;
  if (ssCtrl in Shift) then
  
Modifiers := Modifiers or MOD_CONTROL;
end;

// Register Hotkey
// Hotkey registrieren
procedure TForm1.Button1Click(Sender: TObject);
var
  
Key : Word;
  Modifiers: UINT;
begin
  
ShortCutToHotKey(HotKey1.HotKey, Key, Modifiers);
  Hotkey_id := GlobalAddAtom('MyHotKey_1');
  RegisterHotKey(Handle, Hotkey_id, Modifiers, Key);
end;

// Remove Hotkey
// Hotkey entfernen
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  
UnRegisterHotKey(Handle, Hotkey_id);
  GlobalDeleteAtom(Hotkey_id);
end;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners