定义一个全局快捷键呀,建议你下载一个 HotKeySPY 控件来看看 procedure WMHotKey(var Msg:TMessage); message WM_HOTKEY; public HotKeyId1, HotKeyId2: Integer; HotShift1, HotKey1, HotShift2, HotKey2: Word; // 用于全局热键 //注册热键函数声明 function TMainForm.RegisterMyHotKey(ShortCutStr: String; var HotShift, HotKey: Word; var HotKeyId: Integer): Boolean; var TheShiftState:TShiftState; HotShortCut: TShortCut; function ShiftStateToWord(Shift:TShiftState):Word; begin Result := 0; if ssShift in Shift then Result :=MOD_SHIFT; if ssCtrl in Shift then Result :=Result or MOD_CONTROL; if ssAlt in Shift then Result:=Result or MOD_ALT; end; begin HotShortCut := TextToShortCut(ShortCutStr); ShortCutToKey(HotShortCut, HotKey, TheShiftState); HotShift := ShiftStateToWord(TheShiftState); HotKeyId := GlobalAddAtom(PChar(ShortCutStr)); Result := RegisterHotKey(Self.Handle, HotKeyId, HotShift, HotKey); end;// 处理热键消息 procedure TMainForm.WMHotKey(var Msg: TMessage); begin if (Msg.LparamLo = HotShift1) AND (Msg.LParamHi = HotKey1) then begin if MainForm.Visible then begin Application.Minimize; ShowWindow(MainForm.Handle, SW_HIDE); MainForm.Hide; end else begin MainForm.Show; Application.Restore; ShowWindow(MainForm.Handle, SW_SHOW); SetForegroundWindow(Application.Handle); //Windows.FlashWindow(Self.Handle, True); end; Exit; end; end; //注册全局热键 procedure TMainForm.FormCreate(Sender: TObject); RegisterMyHotKey('Ctrl+Alt+A', HotShift1, HotKey1, HotKeyId1); end; //注销全局热键 procedure TMainForm.FormDestroy(Sender: TObject); if HotKeyId1 >0 then begin UnRegisterHotKey(Self.Handle, HotKeyId1); GlobalDeleteAtom(HotKeyId1); end; end;
procedure WMHotKey(var Msg:TMessage); message WM_HOTKEY;
public
HotKeyId1, HotKeyId2: Integer;
HotShift1, HotKey1, HotShift2, HotKey2: Word; // 用于全局热键
//注册热键函数声明
function TMainForm.RegisterMyHotKey(ShortCutStr: String;
var HotShift, HotKey: Word; var HotKeyId: Integer): Boolean;
var
TheShiftState:TShiftState;
HotShortCut: TShortCut;
function ShiftStateToWord(Shift:TShiftState):Word;
begin
Result := 0;
if ssShift in Shift then Result :=MOD_SHIFT;
if ssCtrl in Shift then Result :=Result or MOD_CONTROL;
if ssAlt in Shift then Result:=Result or MOD_ALT;
end;
begin
HotShortCut := TextToShortCut(ShortCutStr);
ShortCutToKey(HotShortCut, HotKey, TheShiftState);
HotShift := ShiftStateToWord(TheShiftState);
HotKeyId := GlobalAddAtom(PChar(ShortCutStr));
Result := RegisterHotKey(Self.Handle, HotKeyId, HotShift, HotKey);
end;// 处理热键消息
procedure TMainForm.WMHotKey(var Msg: TMessage);
begin
if (Msg.LparamLo = HotShift1) AND (Msg.LParamHi = HotKey1) then
begin
if MainForm.Visible then
begin
Application.Minimize;
ShowWindow(MainForm.Handle, SW_HIDE);
MainForm.Hide; end else begin MainForm.Show;
Application.Restore;
ShowWindow(MainForm.Handle, SW_SHOW);
SetForegroundWindow(Application.Handle);
//Windows.FlashWindow(Self.Handle, True);
end;
Exit;
end;
end;
//注册全局热键
procedure TMainForm.FormCreate(Sender: TObject);
RegisterMyHotKey('Ctrl+Alt+A', HotShift1, HotKey1, HotKeyId1);
end;
//注销全局热键
procedure TMainForm.FormDestroy(Sender: TObject);
if HotKeyId1 >0 then begin
UnRegisterHotKey(Self.Handle, HotKeyId1);
GlobalDeleteAtom(HotKeyId1);
end;
end;