How to write a C program to count the number of mouse clicks in our Pc.Can any one give me idea for that ?

You will need to code a way to listen for mouse clicks (most systems provide an interface to this functionality) and then report the values accumulated over time.

Look at your system documentation for where to begin.

How to write a C program to count the number of mouse clicks in our Pc.Can any one give me idea for that ?

Your question is very vague. For example, what OS are you using? Do you want to count the mouse clicks in one particular application or do you wish to count the system wide mouse clicks?

An example of a system wide Windows mouse click counter follows. As a side benefit it can also track the position of the mouse cursor on the desktop, Just uncomment the two lines to track the mouse cursor position.

#pragma comment( lib, "user32.lib" )

#include <windows.h>
#include <stdio.h>

HHOOK hMouseHook;

__declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
{
    if(wParam == WM_LBUTTONDOWN)
        printf("Left Mouse click\n");
    if(wParam == WM_RBUTTONDOWN)
        printf("Right Mouse click\n");
    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;

}

Hi thank u so much..i actually wish to count the system wide mouse clicks...sure this code will help me to solve the problem. thank u once again :)

Your question is very vague. For example, what OS are you using? Do you want to count the mouse clicks in one particular application or do you wish to count the system wide mouse clicks?

An example of a system wide Windows mouse click counter follows. As a side benefit it can also track the position of the mouse cursor on the desktop, Just uncomment the two lines to track the mouse cursor position.

#pragma comment( lib, "user32.lib" )

#include <windows.h>
#include <stdio.h>

HHOOK hMouseHook;

__declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
{
    if(wParam == WM_LBUTTONDOWN)
        printf("Left Mouse click\n");
    if(wParam == WM_RBUTTONDOWN)
        printf("Right Mouse click\n");
    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;

}
if(wParam == WM_LBUTTONDOWN) {
        counter++;
        printf("Count: $i\n", counter);
    }
    if(wParam == WM_RBUTTONDOWN){
        counter++;
        printf("Count: $i\n", counter);
    }

Is it possible to do the same with Java ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.