首页 > 代码库 > delphi注册热键方法(一)

delphi注册热键方法(一)

uses windows,menus;

.....
//声明
HotKey_Key: Word;
HotKey_Shift: Word;
procedure WMHotKey(var msg : Tmessage); message WM_HOTKEY;

......


function ShiftStateToWord(Shift:TShiftState): Word;
begin
if ssShift in Shift then Result :=MOD_SHIFT else Result := 0;
if ssCtrl in Shift then Result :=Result or MOD_CONTROL;
if ssAlt in Shift then Result:=Result or MOD_ALT;
end;


//实现
procedure TForm1.WMHotKey (var Msg : Tmessage);
begin
if ((Msg.LParamHi = HotKey_Key) and (Msg.LParamLo = HotKey_Shift)) then
begin
//do someting.....
end;

end;

//注册热键
procedure TForm1.hotKey1Change(Sender: TObject);
var SS: TShiftState;
begin
Aatom := GlobalAddAtom(‘EA7276EC-34C1-4A11-8CB3-9940FC76539D‘); //全局热键ID
ShortCutToKey(HotKeyset.HotKey, HotKey_Key, SS);
HotKey_Shift := ShiftStateToWord(SS);
RegisterHotKey(Handle, Aatom, HotKey_Shift, HotKey_Key); //注册全局热键
end;

//取消已注册的热键
procedure TForm1.FormDestroy(Sender: TObject);
begin
UnRegisterHotKey(Handle, Aatom);
GlobalDeleteAtom(Aatom);
end;

delphi注册热键方法(一)