Need Help to Replicate a Click Counter

Dec 3, 2020 at 6:09pm
Guys, they closed the schools but not the school projects.. errgghhh.. sorry for the frustration.. actually I need to submit a c++ assignment this week. I got my idea approved which is a click counter (referred from https://cpstest.pro). This is what I have done till now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma comment( lib, "user32.lib" )
#include <windows.h>
#include <stdio.h>
HHOOK hMouseHook;
__declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
{
   long LCounter = 0;
   long RCounter = 0;
    if(wParam == WM_LBUTTONDOWN)
        LCounter++;
        printf("Left Mouse click count: ", LCounter);
        
    if(wParam == WM_RBUTTONDOWN)
        RCounter++;
        printf("/n Right Mouse click count: ", RCounter);
       
    MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
//    if (pMouseStruct != NULL)
 //       printf("Mouse position X = %d  Mouse Position Y = %d\n", pMouseStruct->pt.x,pMouseStruct->pt.y);
    return CallNextHookEx(hMouseHook,
                          nCode,wParam,lParam);
}
void MessageLoop()
{
    MSG message;
    while (GetMessage(&message,NULL,0,0)) {
        TranslateMessage( &message );
        DispatchMessage( &message );
    }
}
DWORD WINAPI MyMouseLogger(LPVOID lpParm)
{
    HINSTANCE hInstance = GetModuleHandle(NULL);
    if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm);
    if (!hInstance) return 1;
    hMouseHook = SetWindowsHookEx (
                     WH_MOUSE_LL,
                     (HOOKPROC) KeyboardEvent,
                     hInstance,
                     NULL
                 );
    MessageLoop();
    UnhookWindowsHookEx(hMouseHook);
    return 0;
}
int main(int argc, char** argv)
{
    HANDLE hThread;
    DWORD dwThread;
    hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)
                           MyMouseLogger, (LPVOID) argv[0], NULL, &dwThread);
    if (hThread)
        return WaitForSingleObject(hThread,INFINITE);
    else return 1;
}


This is code works just for 1 click... like the counter goes from 0 to 1 after first click and then gets stuck just there no matter how many clicks you make.

Can someone point out what am I doing wrong? Appreciate any help.. need to submit this by Monday.
Last edited on Dec 3, 2020 at 6:37pm
Dec 3, 2020 at 6:24pm
Which of those variables is your count of how many times the mouse has been clicked?
Dec 3, 2020 at 6:37pm
Ohh dang! I pasted the original code that I got from stackoverflow and afterwards added my counter code into it.. updated the post with latest code now... the variables are LCounter, RCounter
Dec 3, 2020 at 6:52pm
Looks like your counters only exist for the duration of the function KeyboardEvent. They get created each time, set to zero, incremented by one, and then destroyed.

You need them to last forever.

What does this do for you, in place of lines 7 and 8 above
1
2
 static long LCounter = 0;
static long RCounter = 0;

Last edited on Dec 3, 2020 at 6:52pm
Dec 3, 2020 at 7:20pm
man this worked! how could I miss that part.. basics gotta be strong... will keep this in mind next time. thanks, mate, you are a savior! Let me just finalize this stuff.. will ask if something goes wrong again.
Last edited on Dec 3, 2020 at 7:21pm
Topic archived. No new replies allowed.