0

I have a C code that checks if the left buttom of the mouse has been pressed. It works fine but I want to use it to count how many times the button has been clicked and call to a function when the button has been clicked a random number of times.

This is the code:

LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    int count = 0;
    MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
    if (pMouseStruct != NULL){
        if (wParam == WM_LBUTTONDOWN)
        {
            count++;
            printf("%d",count);
            if (count==finalNum){ // user clicked random times the mouse so we launch the final function
                printf("\ndone!\n");
                final();

            }
            printf("clicked");
        }
        printf("Mouse position X = %d  Mouse Position Y = %d\n", pMouseStruct->pt.x, pMouseStruct->pt.y);
    }
    return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}

DWORD WINAPI MyMouseLogger(LPVOID lpParm)
{
    HINSTANCE hInstance = GetModuleHandle(NULL);
    // here I put WH_MOUSE instead of WH_MOUSE_LL
    hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, NULL);
    MSG message;

    while (GetMessage(&message, NULL, 0, 0)) {
        TranslateMessage(&message);
        DispatchMessage(&message);
    }

    UnhookWindowsHookEx(hMouseHook);
    return 0;
}

void custom_delay(){

}

int main(int argc, char *argv[])
{
    int count = 0;
    HANDLE hThread;
    DWORD dwThread;
    //////Generate random number to call a function after rand() number of clicks
    srand(time(NULL)); // Seed the time
    int finalNum = rand() % (150 - 50) + 50; // Generate the number, assign to variable.
    ////////////////////////////////////////////////////////////////////////////

    printf("%d", finalNum);
    hThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)MyMouseLogger, (LPVOID)argv[0], NULL, &dwThread);
    if (hThread)
        return WaitForSingleObject(hThread, INFINITE);
    else
        return 1;
    }
}

The problem is that the count variable resets to 0 each time that a mouse event take place so I can't get a track of the times that the user clicks with the mouse.

The other problem is that I would like to generate a random number of times between 50 and 150 to call the final() function. How can I send that random number as an argument?

thank you for your help!

Lundin
  • 195,001
  • 40
  • 254
  • 396
user1618465
  • 1,813
  • 2
  • 32
  • 58

1 Answers1

3

Since you declare count in a function it is allocated when the function is called and automatically deallocated as soon as the function returns, if you want count to last longer you could make it global (declare it outside the function).

Or you use the static key word in the delcaration of count, i.e. static int count = 0. When a variable is declared with static it's allocated for the length of the whole program. This way when the function returns count won't be unallocated. Here's some more info about static -
What does static mean in ANSI-C
http://en.wikipedia.org/wiki/Static_variable

Now for the next part of your question, you can generate a pseudo random number in C by using the rand function. The function rand returns an integer from 0 to RAND_MAX which is a constant defined by the standard library. you can read more about rand here - http://www.cplusplus.com/reference/cstdlib/rand/

Also if you want to store some random number and be able to access it from mouseProc you could give it global scope however be aware that it isn't always a good practice to make all your variables global. http://en.wikipedia.org/wiki/Scope_(computer_science)

Community
  • 1
  • 1
sudowoodo
  • 473
  • 4
  • 14