//给你个热键类,机理就是windows.registerwindowshotkey: unit HotKey_San;interface uses windows,sysutils,classes,messages,controls;type //TOnHotKeyEvent =procedure of object; THotKeyRec=class public GStr:string; ID:integer; //////////// Succeed:boolean; Enabled:boolean; //////////// Modifiers:Cardinal; Key:Cardinal; //////////// Sync:boolean; Handled:boolean; //////////// ExtData:pointer; ExtFigure:real; //////////// OnHotKey:TNotifyEvent; //////////// constructor Create(AModifiers,AKey:Cardinal;AHandleProc:TNotifyEvent); end; //////////////////////////////////////////////////////////////// THotKey_San=class(TWinControl) private protected List:TList; ///////////////// function UnRegisterAHotKey(HKRec:THotKeyRec):boolean; function RegisterAHotKey(HKRec:THotKeyRec;AEnable:boolean=true):boolean; function FindItem(AModifiers,AKey:Cardinal):THotKeyRec; ///////////////// procedure OnGetHotKey(var msg:tmessage);message wm_hotkey; public /////////////// procedure SetEnabled(AEnabled:boolean); function GetHotKeyCount:integer; procedure DeleteAll; function GetHotKeyHandleProc(AModifiers,AKey:Cardinal):TNotifyEvent; function AddHotKey(AModifiers,AKey:Cardinal;AHandleProc:TNotifyEvent;AEnable:boolean=true):THotKeyRec; procedure DeleteHotKey(AModifiers,AKey:Cardinal); procedure EnableHotKey(AModifiers,AKey:Cardinal); procedure DisableHotKey(AModifiers,AKey:Cardinal); ////////// constructor Create(AWinControl: TWinControl); destructor Destroy;override;end;implementation{ THotKey_San }
function THotKey_San.AddHotKey(AModifiers, AKey: Cardinal;AHandleProc:TNotifyEvent;AEnable:boolean=true): THotKeyRec; var t:thotkeyrec; begin t:=thotkeyrec.Create(amodifiers,akey,Ahandleproc); if not registerahotkey(t) then begin t.Free; t:=nil; end else list.Add(t); ///////////// result:=t; end;constructor THotKey_San.Create(AWinControl:TWinControl); begin inherited create(awincontrol); self.Parent:=awincontrol; self.Visible:=false; list:=tlist.Create; end; procedure THotKey_San.DeleteAll; var t:thotkeyrec; begin while list.Count>0 do begin t:=thotkeyrec(list.Items[0]); unregisterahotkey(t); t.Free; list.Delete(0); end; end;procedure THotKey_San.DeleteHotKey(AModifiers, AKey: Cardinal); var t:thotkeyrec; begin t:=finditem(amodifiers,akey); if t<>nil then begin unregisterahotkey(t); list.Delete(list.IndexOf(t)); t.Free; t:=nil; end; end;destructor THotKey_San.Destroy; var t:thotkeyrec; i:integer; begin // beep; for i:=0 to list.Count-1 do begin t:=thotkeyrec(list.Items[i]); unregisterahotkey(t); t.Free; end; list.Free; inherited; end;procedure THotKey_San.DisableHotKey(AModifiers,AKey:Cardinal); var t:thotkeyrec; begin t:=finditem(amodifiers,akey); if t<>nil then t.Enabled:=false; end;procedure THotKey_San.EnableHotKey(AModifiers,AKey:Cardinal); var t:thotkeyrec; begin t:=finditem(amodifiers,akey); if t<>nil then t.Enabled:=true; end;function THotKey_San.FindItem(AModifiers, AKey: Cardinal): THotKeyRec; var i:integer; t:thotkeyrec; begin result:=nil; for i:=0 to list.Count-1 do begin t:=thotkeyrec(list.Items[i]); if (t.Modifiers=amodifiers) and(t.Key=akey) then begin result:=t; exit; end; end; end;function THotKey_San.GetHotKeyCount: integer; begin result:=list.Count; end;function THotKey_San.GetHotKeyHandleProc(AModifiers, AKey: Cardinal): TNotifyEvent; var t:thotkeyrec; begin result:=nil; t:=finditem(amodifiers,akey) ; if t<>nil then result:=t.onhotkey; end;procedure THotKey_San.OnGetHotKey(var msg:tmessage); var t:thotkeyrec; setted:boolean; begin //setted:=false; if not self.Enabled then exit;
t:=finditem(msg.LParamLo,msg.LParamHi); // if t=nil then beep; if (t<>nil)and(t.Succeed)and(t.Enabled) then begin setted:=t.Sync; if setted then t.Enabled:=false; t.onhotkey(t); if t.Handled then msg.Result:=1; if setted then t.Enabled:=true; end; end;function THotKey_San.RegisterAHotKey(HKRec:THotKeyRec;AEnable:boolean=true): boolean; var i:cardinal; begin result:=false; ////////// i:=list.Count; hkrec.gstr:=classname+':'+hkrec.ClassName+'_'+inttostr(i); while GlobalFindAtom(pchar(hkrec.gstr))<>0 do begin hkrec.gstr:=ClassName+':'+hkrec.ClassName+'_'+inttostr(i); inc(i); end; ////////// hkrec.id:=GlobalAddAtom(pchar(hkrec.gstr)); result:=registerhotkey(handle,hkrec.id,hkrec.modifiers,hkrec.key); hkrec.succeed:=result; hkrec.Enabled:=aenable; end;procedure THotKey_San.SetEnabled(AEnabled: boolean); begin enabled:=aenabled; end;function THotKey_San.UnRegisterAHotKey(HKRec:THotKeyRec): boolean; begin result:=UnRegisterHotKey(handle,hkrec.id) and (GlobalDeleteAtom(hkrec.id)=0); end;{ THotKeyRec }constructor THotKeyRec.Create(AModifiers,AKey:Cardinal;AHandleProc:TNotifyEvent); begin self.ID:=0; self.Enabled:=false; self.Succeed:=false; self.GStr:=''; self.Modifiers:=amodifiers; self.Key:=akey; self.onhotkey:=ahandleproc; //////////// handled:=false; extdata:=nil; extfigure:=0; /////////// self.Sync:=true; end;end.
//使用起来极其简单: unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, hotkey_san;type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } hk:thotkey_san; procedure test(sender:tobject); public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject); begin hk:=thotkey_san.Create(self); hk.AddHotKey(mod_shift+mod_alt,ord('S'),test); end;procedure TForm1.test(sender: tobject); begin application.Restore; application.BringToFront; end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin hk.Free; end;end.
中的程序定义了热键有源代码
unit HotKey_San;interface
uses
windows,sysutils,classes,messages,controls;type
//TOnHotKeyEvent =procedure of object;
THotKeyRec=class
public
GStr:string;
ID:integer;
////////////
Succeed:boolean;
Enabled:boolean;
////////////
Modifiers:Cardinal;
Key:Cardinal;
////////////
Sync:boolean;
Handled:boolean;
////////////
ExtData:pointer;
ExtFigure:real;
////////////
OnHotKey:TNotifyEvent;
////////////
constructor Create(AModifiers,AKey:Cardinal;AHandleProc:TNotifyEvent);
end;
////////////////////////////////////////////////////////////////
THotKey_San=class(TWinControl)
private
protected List:TList;
///////////////// function UnRegisterAHotKey(HKRec:THotKeyRec):boolean;
function RegisterAHotKey(HKRec:THotKeyRec;AEnable:boolean=true):boolean;
function FindItem(AModifiers,AKey:Cardinal):THotKeyRec;
/////////////////
procedure OnGetHotKey(var msg:tmessage);message wm_hotkey;
public ///////////////
procedure SetEnabled(AEnabled:boolean);
function GetHotKeyCount:integer;
procedure DeleteAll;
function GetHotKeyHandleProc(AModifiers,AKey:Cardinal):TNotifyEvent;
function AddHotKey(AModifiers,AKey:Cardinal;AHandleProc:TNotifyEvent;AEnable:boolean=true):THotKeyRec;
procedure DeleteHotKey(AModifiers,AKey:Cardinal);
procedure EnableHotKey(AModifiers,AKey:Cardinal);
procedure DisableHotKey(AModifiers,AKey:Cardinal);
//////////
constructor Create(AWinControl: TWinControl);
destructor Destroy;override;end;implementation{ THotKey_San }
function THotKey_San.AddHotKey(AModifiers, AKey: Cardinal;AHandleProc:TNotifyEvent;AEnable:boolean=true): THotKeyRec;
var
t:thotkeyrec;
begin
t:=thotkeyrec.Create(amodifiers,akey,Ahandleproc);
if not registerahotkey(t) then
begin
t.Free;
t:=nil;
end
else
list.Add(t);
/////////////
result:=t;
end;constructor THotKey_San.Create(AWinControl:TWinControl);
begin
inherited create(awincontrol);
self.Parent:=awincontrol;
self.Visible:=false;
list:=tlist.Create;
end;
procedure THotKey_San.DeleteAll;
var
t:thotkeyrec;
begin
while list.Count>0 do
begin
t:=thotkeyrec(list.Items[0]);
unregisterahotkey(t);
t.Free;
list.Delete(0);
end;
end;procedure THotKey_San.DeleteHotKey(AModifiers, AKey: Cardinal);
var
t:thotkeyrec;
begin
t:=finditem(amodifiers,akey);
if t<>nil then
begin
unregisterahotkey(t);
list.Delete(list.IndexOf(t));
t.Free;
t:=nil;
end;
end;destructor THotKey_San.Destroy;
var
t:thotkeyrec;
i:integer;
begin
// beep;
for i:=0 to list.Count-1 do
begin
t:=thotkeyrec(list.Items[i]);
unregisterahotkey(t);
t.Free;
end;
list.Free;
inherited;
end;procedure THotKey_San.DisableHotKey(AModifiers,AKey:Cardinal);
var
t:thotkeyrec;
begin
t:=finditem(amodifiers,akey);
if t<>nil then
t.Enabled:=false;
end;procedure THotKey_San.EnableHotKey(AModifiers,AKey:Cardinal);
var
t:thotkeyrec;
begin
t:=finditem(amodifiers,akey);
if t<>nil then
t.Enabled:=true;
end;function THotKey_San.FindItem(AModifiers, AKey: Cardinal): THotKeyRec;
var
i:integer;
t:thotkeyrec;
begin
result:=nil;
for i:=0 to list.Count-1 do
begin
t:=thotkeyrec(list.Items[i]);
if (t.Modifiers=amodifiers) and(t.Key=akey) then
begin
result:=t;
exit;
end;
end;
end;function THotKey_San.GetHotKeyCount: integer;
begin
result:=list.Count;
end;function THotKey_San.GetHotKeyHandleProc(AModifiers, AKey: Cardinal): TNotifyEvent;
var
t:thotkeyrec;
begin
result:=nil;
t:=finditem(amodifiers,akey) ;
if t<>nil then
result:=t.onhotkey;
end;procedure THotKey_San.OnGetHotKey(var msg:tmessage);
var
t:thotkeyrec;
setted:boolean;
begin
//setted:=false;
if not self.Enabled then exit;
t:=finditem(msg.LParamLo,msg.LParamHi);
// if t=nil then beep;
if (t<>nil)and(t.Succeed)and(t.Enabled) then
begin
setted:=t.Sync;
if setted then t.Enabled:=false;
t.onhotkey(t);
if t.Handled then msg.Result:=1;
if setted then t.Enabled:=true; end; end;function THotKey_San.RegisterAHotKey(HKRec:THotKeyRec;AEnable:boolean=true): boolean;
var
i:cardinal;
begin
result:=false;
////////// i:=list.Count; hkrec.gstr:=classname+':'+hkrec.ClassName+'_'+inttostr(i);
while GlobalFindAtom(pchar(hkrec.gstr))<>0 do
begin
hkrec.gstr:=ClassName+':'+hkrec.ClassName+'_'+inttostr(i);
inc(i);
end;
//////////
hkrec.id:=GlobalAddAtom(pchar(hkrec.gstr));
result:=registerhotkey(handle,hkrec.id,hkrec.modifiers,hkrec.key);
hkrec.succeed:=result;
hkrec.Enabled:=aenable;
end;procedure THotKey_San.SetEnabled(AEnabled: boolean);
begin
enabled:=aenabled;
end;function THotKey_San.UnRegisterAHotKey(HKRec:THotKeyRec): boolean;
begin
result:=UnRegisterHotKey(handle,hkrec.id) and (GlobalDeleteAtom(hkrec.id)=0);
end;{ THotKeyRec }constructor THotKeyRec.Create(AModifiers,AKey:Cardinal;AHandleProc:TNotifyEvent);
begin
self.ID:=0;
self.Enabled:=false;
self.Succeed:=false;
self.GStr:='';
self.Modifiers:=amodifiers;
self.Key:=akey;
self.onhotkey:=ahandleproc;
////////////
handled:=false;
extdata:=nil;
extfigure:=0;
///////////
self.Sync:=true;
end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, hotkey_san;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
hk:thotkey_san;
procedure test(sender:tobject);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
hk:=thotkey_san.Create(self); hk.AddHotKey(mod_shift+mod_alt,ord('S'),test);
end;procedure TForm1.test(sender: tobject);
begin
application.Restore;
application.BringToFront;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
hk.Free;
end;end.