注册系统热键

unit unitFrmMain;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
 TForm1 = class(TForm)
   procedure FormCreate(Sender: TObject);
   procedure FormClose(Sender: TObject; var Action: TCloseAction);
 private
   { Private declarations }
   hotkeyid: integer;
   procedure WMhotkeyhandle(var msg: Tmessage);
     message WM_HOTKEY; //响应热键消息
 public
   { Public declarations }
 end;

var
 Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
 hotkeyid := GlobalAddAtom(pchar
   ('UserDefineHotKey')) - $C000;
   //减去$C000是为了保证取值范围的限制
 registerhotkey(handle, hotkeyid,
    MOD_ALT or MOD_CONTROL, $42);//热键为ctrl+Alt+B
end;

procedure TForm1.WMhotkeyhandle(var msg: Tmessage);

begin
 if (msg.LParamHi = $42) and
   (msg.lparamLo = MOD_CONTROL or mod_Alt)
 then
 begin
   msg.Result := 1; //该消息已经处理
   application.BringToFront;
   ShowMessage('你已经按下热键')
 end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 unregisterhotkey(handle, hotkeyid);
 DeleteAtom(HotKeyID);
end;

end.

你可能感兴趣的:(注册系统热键)