1.在窗启动时创建ATOM;(aatom:ATOM;定义在private中)
1 if FindAtom('ZWXhotKey')=0 then 2 begin 3 aatom:=GlobalAddAtom('ZWXhotKey'); 4 end; 5 if RegisterHotKey(Handle,aatom,MOD_ALT,$41) then 6 begin 7 MessageBox(Handle,'按alt+a','提示',MB_OK); 8 end;
2.定义处理热键的消息过程(定义在private中,下面二个处理消息的过程是一样的)
1 procedure hotkey(var msg:TMessage);message WM_HOTKEY;//定义全局热键消息事件 2 //procedure hotkey2(var msg:TWMHotKey);message WM_HOTKEY;//同上
3.消息过程的处理(下面二个IF任选一个即可,如果msg在步骤2定义成TWMHotKey,则不用转换)
1 procedure TForm2.hotkey(var msg: TMessage); 2 begin 3 if TWMHotKey(msg).HotKey=aatom then 4 begin 5 //ShowMessage('s'); 6 end; 7 if (msg.LParamHi=$41) and (msg.LParamLo=MOD_ALT) then 8 begin
//處理事情 13 end; 14 end;
4.程序关闭时,刪除热键和原子
1 procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction); 2 begin 3 UnregisterHotKey(Handle,aatom); 4 GlobalDeleteAtom(aatom); 5 end;