Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Installing win32 hook to get system wide mouse clicks

Status
Not open for further replies.

titanandrews

Programmer
Feb 27, 2003
130
0
0
US
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



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 <<  &quot; x: &quot; << x << &quot; y: &quot; << y << endl;
    
    
    LRESULT RetVal = CallNextHookEx( hmb, nCode, wParam, lParam );  
    
    return (RetVal);

}//end mouseProc() function


BOOL __declspec(dllexport)__stdcall installhook()
{       
    string mouseFile = getenv(&quot;USERPROFILE&quot;);
    mouseFile.append(&quot;\\&quot;).append(&quot;Report.txt&quot;);
    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);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top