titanandrews
Programmer
Hi,
I am trying to install a win32 hook that receives all mouse clicks and movements, but I have some difficulty. It seems that in the switch in the sample code below, I only get an event if the mouse click is on the command prompt window. (very odd) And I don't get any WM_NCMOUSEMOVE events unless my cursor leaves the command prompt window. Also it seems that mouse coordinates are not relative to the screen, but possibly relative to again the command prompt. (I think). In the installhook() function, I call SetWindowsHookEx() with 0 as the last parameter. MSDN says to use this if you want a system wide hook.
Obviously I am doing something wrong here, but the MSDN documentation is not very clear in this area. If anyone has any experience in this and can offer some suggestions, I would love to hear.
many thanks,
Barry
I am trying to install a win32 hook that receives all mouse clicks and movements, but I have some difficulty. It seems that in the switch in the sample code below, I only get an event if the mouse click is on the command prompt window. (very odd) And I don't get any WM_NCMOUSEMOVE events unless my cursor leaves the command prompt window. Also it seems that mouse coordinates are not relative to the screen, but possibly relative to again the command prompt. (I think). In the installhook() function, I call SetWindowsHookEx() with 0 as the last parameter. MSDN says to use this if you want a system wide hook.
Obviously I am doing something wrong here, but the MSDN documentation is not very clear in this area. If anyone has any experience in this and can offer some suggestions, I would love to hear.
many thanks,
Barry
Code:
LRESULT __declspec(dllexport)__stdcall CALLBACK mouseProc(
int nCode,
WPARAM wParam,
LPARAM lParam)
{
string myButton;
LPMOUSEHOOKSTRUCT MouseParam;
static int x,y;
if(nCode ==0)
{
MouseParam= (MOUSEHOOKSTRUCT *) lParam;
if (MouseParam != NULL)
{
x = MouseParam->pt.x;
y = MouseParam->pt.y;
}
switch (wParam)
{
case WM_NCLBUTTONDOWN:
myButton = "Left button down";
break;
case WM_NCRBUTTONDOWN:
myButton = "Right button down";
break;
case WM_NCLBUTTONUP:
myButton = "Left button up";
break;
case WM_NCRBUTTONUP:
myButton = "Right button up";
break;
case WM_NCLBUTTONDBLCLK:
myButton = "Left double click";
break;
case WM_NCMOUSEMOVE:
myButton = "Mouse move";
break;
}
}
mouseReport << myButton << " x: " << x << " y: " << y << endl;
LRESULT RetVal = CallNextHookEx( hmb, nCode, wParam, lParam );
return (RetVal);
}//end mouseProc() function
BOOL __declspec(dllexport)__stdcall installhook()
{
string mouseFile = getenv("USERPROFILE");
mouseFile.append("\\").append("Report.txt");
mouseReport.open(mouseFile.c_str(),ios::out | ios::app);
//0 for the last parameter should mean do not use a threadid, but this hook is for system.
hmb=SetWindowsHookEx(WH_MOUSE,(HOOKPROC)mouseProc,hins,0);
return (TRUE);
}