0

My requirement is that: I want to detect left mouse click event and take some action. Mouse click should get detected only on the given application if mouse click is on other application then it should not take action.

Currently I am using :

mousehook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);

LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{

    PKBDLLHOOKSTRUCT k = (PKBDLLHOOKSTRUCT)(lParam);
    POINT p;

    if (wParam == WM_LBUTTONDOWN)
    {
        // MB1 click
    }
    return CallNextHookEx(0, nCode, wParam, lParam);
}

but it is working for mouse click anywhere on the desktop screen. I want to detect it for current application only.

2
  • Might want to take a look here: SetWindowsHookEx - description of parameter dwThreadId Commented Dec 14, 2017 at 13:03
  • @Blacktempel: Low-level hooks can only be global. They run before the target window/thread has even been evaluated. Naturally, you cannot filter low-level hooks on data that's not available. Commented Jun 18, 2018 at 9:29

1 Answer 1

3

If you have Win32 application. Probably you can handle mouse events in your Window proc itself.

     LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     {
      switch (message)
      {
        case WM_LBUTTONDOWN:
          // your code
          return 0;

        case WM_LBUTTONUP:
          // your code
          return 0;

        case WM_DESTROY:
          PostQuitMessage(0);
          return 0;
      }
      return DefWindowProc(hwnd, message, wParam, lParam);
    }

Window creation:

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("Connect");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    if (!RegisterClass(&wndclass)) {
        MessageBox(NULL, TEXT("Program requires Windows NT!"), szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(szAppName, TEXT("Connect−the−Points Mouse Demo"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, NULL, hInstance, NULL);

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
Sign up to request clarification or add additional context in comments.

4 Comments

But how it confirm that the click is in the current window / application only Not Outside?
Because now we are capturing mouse events for Window, not by global mouse events hook. Give a try, and let me know if it doesn't work. Read a book by Charles Petzold for core concepts of Win32 programming.
One question where we register this callback?
updated the answer, check the window creation code (wndclass.lpfnWndProc = WndProc)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.