定义全局热键

 1 BOOL RegisterHotKey(      
2 HWND hWnd,    {窗口句柄}
3 int id,       {热键标识}
4 UINT fsModifiers, {热键模式,是否按下MOD_WIN,MOD_SHIFT,MOD_CONTROL,MOD_ALT}
5 UINT vk      {热键虚拟键}
6 );
7
8 {定义一个全局变量}
9
10 MY_ID: Integer;              {全局因子}
11 procedure WMHotKey(var Msg :TWMHotKey); message WM_HOTKEY;  {捕获Windows消息的 WM_HOTKEY的函数}
12
13 procedure TForm1.WMHotKey (var Msg :TWMHotKey); 
14 begin
15 if msg.HotKey = MY_ID then
16 form2.Visible := true;    
17 end;
18
19 procedure TForm1.FormCreate(Sender: TObject);
20 begin
21 if GlobalFindAtom('MY_IDHotKey') = 0 then {首先全局因子是否存在}
22 begin
23 MY_ID:=GlobalAddAtom('MY_IDHotKey'); {生成全局因子}
24 RegisterHotKey(handle,MY_ID,MOD_ALT+MOD_CONTROL,82); {注册全局热键Ctrl+Alt+R,82是R的虚拟键值}
25 end
26 else
27 halt; {退出}
28 end;
29
30 procedure TForm1.FormDestroy(Sender: TObject);
31 begin
32 UnRegisterHotKey(handle,MY_ID);   {注销热键}
33 GlobalDeleteAtom(MY_ID);        {删除全局因子}
34 end;

你可能感兴趣的:(定义全局热键)