送你一个hook的例子:
unit HKProc;
interface
uses
Windows, Messages;
var
hNextHookProc: HHook;
procSaveExit: Pointer;
function KeyboardHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT;
stdcall;
export;
function EnableHotKeyHook: BOOL;
export;
function DisableHotKeyHook: BOOL;
export;
procedure HotKeyHookExit;
far;
implementation
function KeyboardHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT;
stdcall;
export;
const
_KeyPressMask = $80000000;
begin
Result := 0;
If iCode < 0 then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end;
// 侦测 Ctrl + B 组合键
if ((lParam and _KeyPressMask) = 0) and //(lparam and $80000000= 0) -->down <>0 --> up;
(GetKeyState(vk_control) <0) and (wParam = Ord('B')) then
begin
Result := 1;
WinExec('Notepad.exe', sw_Normal);
// 记事本
end;
end;
function EnableHotKeyHook: BOOL;
export;
begin
Result := False;
if hNextHookProc <> 0 then
Exit;
// 挂上 WH_KEYBOARD 这型的 HOOK, 同时, 传回值必须保留下
// 来, 免得 HOOK 呼叫链结断掉
hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,
KeyboardHookHandler,
HInstance,
0);
Result := hNextHookProc <> 0;
end;
function DisableHotKeyHook: BOOL;
export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc);
// 解除 Keyboard Hook
hNextHookProc := 0;
MessageBeep(0);
MessageBeep(0);
end;
Result := hNextHookProc = 0;
end;
procedure HotKeyHookExit;
begin
// 如果忘了解除 HOOK, 自动代理解除的动作
if hNextHookProc <> 0 then
DisableHotKeyHook;
ExitProc := procSaveExit;
end;
end.