最简单的hook问题,欢迎大家参与.
我想在键盘钩子中监控键盘按键,比如随时监控用户是否按了ctrl+alt+a组合键,我在钩子函数中应该怎么判断呢?
function keyHookProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT;stdcall;
begin
这个地方怎么判断?
end
[解决办法]
call GetAsyncKeyState
[解决办法]
library KeyBoard;
uses
SysUtils,
Windows,
ShellApi,
Messages,
WinProcs,
Classes;
var
KBHook:HHook;
IsHooked:boolean;
function KeyBoardProc(Code:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
const
_keypressmask=$80000000;
begin
result:=0;
if Code <0 then
begin
CallNextHookEx(KBHook,Code,wParam,lParam);
end;
if ((lparam and _keypressmask)=0) and (GetAsyncKeyState(VK_CONTROL) <0) and (GetAsyncKeyState(VK_MENU) <0) and (wParam=ord( 'A ')) then
begin
ShellExecute(0, 'Open ', 'NotePad.exe ',nil,nil,SW_SHOW);
result:=1;
end;
end;
function StartHook:boolean;stdcall;
begin
result:=false;
if IsHooked then exit;
KBHook:=SetWindowsHookEx(WH_KEYBOARD,@KeyBoardProc,hInstance,0);
if KBHook <> 0 then
begin
Result:=true;
IsHooked:=true
end
else
IsHooked:=false;
end;
function RemoveHook:boolean;stdcall;
begin
result:=false;
if (not IsHooked) and (KBHook <> 0) then exit;
UnHookWindowsHookEx(KBHook);
Result:=true;
IsHooked:=false;
end;
exports
StartHook,RemoveHook;
{$R *.res}
begin
end.