0

I want the messagebox to appear whenever I click my mouse, how do I achieve it? do mouse clicks have virtual key code like keyboard presses do? Do I also need to use lparam with MouseHookStruct, please help me with the Callback Function. Nothing happens when I click the mouse.

#ifdef __WIN32
#include<windows.h>

HHOOK __hook;

MOUSEHOOKSTRUCT mouseHookStruct;

KBDLLHOOKSTRUCT keyboardHookStruct;

LRESULT __stdcall HookCallBack(int nCode, WPARAM wparam, LPARAM lparam){

    if(nCode >= 0){

        if(wparam == WM_LBUTTONDOWN)
        {
            mouseHookStruct = *((MOUSEHOOKSTRUCT*) lparam);
            MessageBox(NULL, "left button", "left button", MB_ICONINFORMATION);
        }
        if(wparam == WM_RBUTTONDOWN){
            mouseHookStruct = *((MOUSEHOOKSTRUCT*) lparam);
            MessageBox(NULL, "left button", "left button", MB_ICONINFORMATION); 
        }
    }

    return CallNextHookEx(__hook, nCode, wparam, lparam);
}


void startHook(){

    __hook = SetWindowsHookEx(WH_MOUSE_LL , HookCallBack , NULL , 0);

}

void releaseHook(){
    UnhookWindowsHookEx(__hook);
}
void main(){
 startHook();
}
#endif
  • 1
    You asked the same question an hour ago https://stackoverflow.com/questions/45190578/mousehookproc-in-c (10k+ reps required). Don't repost questions! – too honest for this site Jul 19 '17 at 14:15
  • yes i deleted it and asked again no answer I got :( –  Jul 19 '17 at 14:16
  • Okay was by mistake –  Jul 19 '17 at 14:16
  • 1) We don'tr have a time limite for answers here. 2) If you don't get an answer after 1 year, it still does not allow to repost. – too honest for this site Jul 19 '17 at 14:17
  • Have you read the documentation on LowLevelMouseProc (https://msdn.microsoft.com/en-us/library/windows/desktop/ms644986(v=vs.85).aspx). You aren't following the rules about return values. Also, if you take too long to process the message "on Windows 7 and later, the hook is silently removed without being called.". MessageBox is surely going to take more than the few milliseconds that is allowed. – Neil Jul 19 '17 at 14:22
  • 3
    Your program exits immediately after installing the hook; how do you expect the callback to be called? You need a loop (specifically, a message queue) to actually receive the events. See https://stackoverflow.com/questions/7458807/why-must-setwindowshookex-be-used-with-a-windows-message-queue – Andrew Sun Jul 19 '17 at 14:24
  • I am adding MSG message and then start getmessage function in a while loop then. –  Jul 19 '17 at 14:26
  • It worked when I added the loop, thanks Andrew. But Ihad to restart the computer because whenever I clicked to close the messagebox a new appeared ))) so i was never able to close it also my computer froze –  Jul 19 '17 at 14:33
  • 1
    simply not use MessageBox inside `HookCallBack`. log the clicks by another way – RbMm Jul 19 '17 at 14:37
  • yeap.... I know –  Jul 19 '17 at 14:37

0 Answers0