这个要全局的吧,GetKeyState 好像不是全局的。<br>dll 部分:<br>unit HKProc;<br><br>interface<br><br>uses<br> Windows, Messages;<br><br>var<br> hNextHookProc: HHook;<br> procSaveExit: Pointer;<br><br><br>function KeyboardHookHandler(iCode: Integer;<br> wParam: WPARAM;<br> lParam: LPARAM): LRESULT; stdcall; export;<br>function EnableHotKeyHook: BOOL; export;<br>function DisableHotKeyHook: BOOL; export;<br>procedure HotKeyHookExit; far;<br><br>implementation<br><br>function KeyboardHookHandler(iCode: Integer;<br> wParam: WPARAM;<br> lParam: LPARAM): LRESULT; stdcall; export;<br>const<br> _KeyPressMask = $80000000;<br>begin<br> Result := 0;<br> If iCode < 0 Then<br> begin<br> Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);<br> Exit;<br> end;<br> // 盎代 Ctrl + B 舱?龄<br> if ((lParam and _KeyPressMask) = 0) and<br> (GetKeyState(vk_Control) < 0) then<br> begin<br> MessageBox('Ctrl Is Down');<br> end;<br>end;<br><br><br>function EnableHotKeyHook: BOOL; export;<br>begin<br> Result := False;<br> if hNextHookProc <> 0 then Exit;<br> // 本? WH_KEYBOARD 硂?? HOOK, ??, 肚??ゲ斗玂痙?<br> // ㄓ, ?眔 HOOK ㊣?渺挡耞奔<br> hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,<br> KeyboardHookHandler,<br> HInstance,<br> 0);<br> Result := hNextHookProc <> 0;<br>end;<br><br><br>function DisableHotKeyHook: BOOL; export;<br>begin<br> if hNextHookProc <> 0 then<br> begin<br> UnhookWindowshookEx(hNextHookProc); // 秆埃 Keyboard Hook<br> hNextHookProc := 0;<br> MessageBeep(0);<br> MessageBeep(0);<br> end;<br> Result := hNextHookProc = 0;<br>end;<br><br><br>procedure HotKeyHookExit;<br>begin<br> // ?狦а?秆埃 HOOK, ?笆?瞶秆埃?笆?<br> if hNextHookProc <> 0 then DisableHotKeyHook;<br> ExitProc := procSaveExit;<br>end;<br><br>end.<br>Exe 部分:<br>unit HookU;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> Memo1: TMemo;<br> Label1: TLabel;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>function EnableHotKeyHook: BOOL; external 'HKTEST.DLL';<br>function DisableHotKeyHook: BOOL; external 'HKTEST.DLL';<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> if EnableHotKeyHook then<br> ShowMessage('Keyboard Hook 币笆, ?? Ctrl + B 盢???癘ㄆセ?');<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> if DisableHotKeyHook then<br> ShowMessage('Keyboard Hook ?秆埃!');<br>end;<br><br>end.<br><br><br><br><br>