L
lizheng
Unregistered / Unconfirmed
GUEST, unregistred user!
var
HookForm: THookForm;
hNextHookProc: HHook;
procSaveExit: Pointer;
implementation
{$R *.DFM}
function KeyboardHookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
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;
HookForm.Visible := True;
~~~~~~~~~~~~~~~~~~~~~~~~~`只要执行就出错,不知原因?
HookForm.BringToFront;
end;
end;
function THookForm.DisableHotKeyHook: BOOL;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc); // 解除 Keyboard Hook
hNextHookProc := 0;
MessageBeep(0);
end;
Result := hNextHookProc = 0;
end;
function THookForm.EnableHotKeyHook: BOOL;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
// 挂上 WH_KEYBOARD 这型的 HOOK, 同时, 传回值必须保留下
// 来, 免得 HOOK 呼叫链结断掉
hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,
KeyboardHookHandler,
HInstance,
0);
MessageBeep(0);
Result := hNextHookProc <> 0;
end;
procedure THookForm.FormDestroy(Sender: TObject);
begin
// 如果忘了解除 HOOK, 自动代理解除的动作
if hNextHookProc <> 0 then DisableHotKeyHook;
ExitProc := procSaveExit;
end;
procedure THookForm.SetHookBtnClick(Sender: TObject);
begin
if EnableHotKeyHook then
begin
ShowMessage('Keyboard Hook 启动, 按下 Ctrl + B 将叫出「记事本」');
Visible := False;
end;
end;
procedure THookForm.Button2Click(Sender: TObject);
begin
if DisableHotKeyHook then
ShowMessage('Keyboard Hook 已解除!');
end;
end.
HookForm: THookForm;
hNextHookProc: HHook;
procSaveExit: Pointer;
implementation
{$R *.DFM}
function KeyboardHookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
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;
HookForm.Visible := True;
~~~~~~~~~~~~~~~~~~~~~~~~~`只要执行就出错,不知原因?
HookForm.BringToFront;
end;
end;
function THookForm.DisableHotKeyHook: BOOL;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc); // 解除 Keyboard Hook
hNextHookProc := 0;
MessageBeep(0);
end;
Result := hNextHookProc = 0;
end;
function THookForm.EnableHotKeyHook: BOOL;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
// 挂上 WH_KEYBOARD 这型的 HOOK, 同时, 传回值必须保留下
// 来, 免得 HOOK 呼叫链结断掉
hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,
KeyboardHookHandler,
HInstance,
0);
MessageBeep(0);
Result := hNextHookProc <> 0;
end;
procedure THookForm.FormDestroy(Sender: TObject);
begin
// 如果忘了解除 HOOK, 自动代理解除的动作
if hNextHookProc <> 0 then DisableHotKeyHook;
ExitProc := procSaveExit;
end;
procedure THookForm.SetHookBtnClick(Sender: TObject);
begin
if EnableHotKeyHook then
begin
ShowMessage('Keyboard Hook 启动, 按下 Ctrl + B 将叫出「记事本」');
Visible := False;
end;
end;
procedure THookForm.Button2Click(Sender: TObject);
begin
if DisableHotKeyHook then
ShowMessage('Keyboard Hook 已解除!');
end;
end.