1,183
社区成员
发帖
与我相关
我的任务
分享
unit LLKBMouthHook;
interface
uses
Windows, Messages, SysUtils;
function StartHookKeyMouse: boolean;
function StopHookKeyMouse: boolean;
implementation
type
tagKBDLLHOOKSTRUCT = packed record
vkCode: DWORD;
scanCode: DWORD;
flags: DWORD;
time: DWORD;
dwExtraInfo: DWORD;
end;
KBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT;
PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;
type
PMSLLHOOKSTRUCT = ^MSLLHOOKSTRUCT;
MSLLHOOKSTRUCT = record
pt: TPoint;
hwnd:HWND;
mouseData: DWORD;
flags: DWORD;
time: DWORD;
dwExtraInfo: DWORD;
end;
const
WH_KEYBOARD_LL = 13;
WH_MOUSE_LL = 14;
var
hhkLowLevelKybd: HHook = 0;
hhkLowLevelMouse: HHook = 0;
procedure WriteScreen(TmpStr:string);
var
ScreenDC: HDC;
begin
// 输出到屏幕
ScreenDC := GetDC(0);
TextOut(ScreenDC, 0, 0, PChar(TmpStr), Length(TmpStr));
ReleaseDC(0, ScreenDC);
end;
// 取窗口类名
function GetWinClass(hWnd: HWnd): string;
var
szClassName: array[0..20] of Char;
begin
szClassName[GetClassName(hWnd, szClassName, 20)] := #0;
Result := szClassName;
end;
// 取窗体文字
function GetWinTitle(hWnd: HWnd): string;
var
szWindowText: array[0..20] of Char;
szTextLength: Integer;
begin
szTextLength := SendMessage(hWnd, WM_GETTEXT, 20, Integer(@szWindowText[0]));
szWindowText[szTextLength] := #0;
Result := szWindowText;
end;
function LowLevelKeyboardProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
var
p: PKBDLLHOOKSTRUCT;
fEatKeystroke: BOOL;
begin
if (nCode = HC_ACTION) then
begin
p := PKBDLLHOOKSTRUCT(lParam);
case wParam of
WM_KEYDOWN:
begin
//键盘按下事件
if p.vkCode = VK_RETURN then WriteScreen('回车按下');
end;
end;
end;
Result := 0;
Result := CallNextHookEx(0, nCode, WParam, LParam);
end;
function LowLevelMouseProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
var
p: PMSLLHOOKSTRUCT;
begin
if (nCode = HC_ACTION) then
begin
p := PMSLLHOOKSTRUCT(lParam);
case wParam of
WM_LBUTTONDOWN:
begin
if p.hwnd<>0 then Writescreen('有句柄') else WriteScreen('无句柄');
//无论鼠标点什么程序上的按钮,都取不到鼠标下控件的句柄,为什么呢?
end;
end;
Result := 0;
Result := CallNextHookEx(0, nCode, WParam, LParam);
end;
end;
function StartHookKeyMouse: boolean;
begin
if hhkLowLevelKybd = 0 then hhkLowLevelKybd := SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, Hinstance, 0);
if hhkLowLevelMouse = 0 then hhkLowLevelMouse := SetWindowsHookEx(WH_MOUSE_LL, LowlevelMouseProc, HInstance, 0);
result := (hhkLowLevelKybd <> 0) or (hhkLowLevelMouse <> 0);
end;
function StopHookKeyMouse: boolean;
begin
if hhkLowLevelKybd <> 0 then
begin
UnhookWindowsHookEx(hhkLowLevelKybd);
hhkLowLevelKybd := 0;
end;
if hhkLowLevelMouse <> 0 then
begin
UnHookWindowsHookEx(hhkLowLevelMouse);
hhkLowLevelMouse := 0;
end;
result := true;
end;
initialization
hhkLowLevelKybd := 0;
hhkLowLevelMouse := 0;
finalization
if hhkLowLevelKybd <> 0 then UnhookWindowsHookEx(hhkLowLevelKybd);
if hhkLowLevelMouse <> 0 then UnhookWindowsHookEx(hhkLowLevelMouse);
end.
function LowLevelMouseProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
var
EditHwnd:Hwnd;
begin
if (nCode = HC_ACTION) then
begin
case wParam of
WM_LBUTTONDOWN,WM_LBUTTONUP:
begin
EditHwnd:= WindowFromPoint(PMSLLHOOKSTRUCT(lParam)^.pt);
function LowLevelMouseProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
var
p: PMSLLHOOKSTRUCT;
begin
if (nCode = HC_ACTION) then
begin
//p := ^PMSLLHOOKSTRUCT(lParam);
case wParam of
WM_LBUTTONDOWN:
begin
if PMSLLHOOKSTRUCT(lParam)^.hwnd<>0 then Writescreen('有句柄') else WriteScreen('无句柄');
//无论鼠标点什么程序上的按钮,都取不到鼠标下控件的句柄,为什么呢?
end;
end;
Result := 0;
Result := CallNextHookEx(0, nCode, WParam, LParam);
end;
end;
typedef struct tagMSLLHOOKSTRUCT {
POINT pt;
DWORD mouseData;
DWORD flags;
DWORD time;
ULONG_PTR dwExtraInfo;
} MSLLHOOKSTRUCT, *PMSLLHOOKSTRUCT, *LPMSLLHOOKSTRUCT;
MSDN上的结构声明,并没有句柄……
PMouseHookStruct = ^TMouseHookStruct;
{$EXTERNALSYM tagMOUSEHOOKSTRUCT}
tagMOUSEHOOKSTRUCT = packed record
pt: TPoint;
hwnd: HWND; //这个就是句柄
wHitTestCode: UINT;
dwExtraInfo: DWORD;
end;
TMouseHookStruct = tagMOUSEHOOKSTRUCT;