unit LLKBMouthHook;interfaceuses
Windows, Messages, SysUtils;function StartHookKeyMouse: boolean;
function StopHookKeyMouse: boolean;implementationtype
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.
为什么鼠标处的句柄一直都是找不到的状态。。??
解决方案 »
- 请问delphi有没有类似于c的format函数:'the sum is:%d',getsum
- 100 分请各位老大把下面的VB代码翻译成Delphi
- 在delphi里面显示另一个应用程式
- 请教:关于在delphi中poxpro数据库的用法
- 请问mysql数据库存储过程怎样自动执行?
- 哪位大侠有关于tms charts 的例子
- Tobjectlist类在哪个单元里Delphi2010的Generics.Collections里居然没有!
- 跪求delphi与word之间的交互
- 100奉送!关于使用HP LaserJet 1000 打印机碰到的问题!!!up也有分*************(斑竹也应该来瞧瞧!!!!)
- Win98下面有rnaui.dll,Win2000中是哪个文件???
- webbrowser禁止弹出对话框,用什么方法
- 如何把条形码字体添加到DELPHI中?求高手帮忙!
POINT pt;
DWORD mouseData;
DWORD flags;
DWORD time;
ULONG_PTR dwExtraInfo;
} MSLLHOOKSTRUCT, *PMSLLHOOKSTRUCT, *LPMSLLHOOKSTRUCT;MSDN上的结构声明,并没有句柄啊
{$EXTERNALSYM tagMOUSEHOOKSTRUCT}
tagMOUSEHOOKSTRUCT = packed record
pt: TPoint;
hwnd: HWND; //这个就是句柄
wHitTestCode: UINT;
dwExtraInfo: DWORD;
end;
TMouseHookStruct = tagMOUSEHOOKSTRUCT;
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上的结构声明,并没有句柄……
我查 MSDN 上也是没有 HWND 的声明。 HWND 是我自己按照普通鼠标钩子的格式添加上去的。。是不是真不能取鼠标处句柄哦!~
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);
WindowFromPoint 不纠结了,加了这个函数,转换一下!!!!!~~~ 等两天看有高论没。。 稍后结贴!~