c++windowsclickmousemouse-hook

How to disable right-click button on windows using C++?


Is there a way to disable the right-click button?

I'm trying to use a mouse hook, that just simply disables the right-click button when you run the program.

#include <iostream>
#include <vector>
#include <algorithm>

#include <cstdlib>
#include <conio.h>
#define _WIN32_WINNT 0x0500
#define WM_RBUTTONDOWN 0x0204
#include <windows.h>

/* Disable mouse using low-level mouse hook */
HHOOK miHook;

    //Starting Hook Procedure
    LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode == HC_ACTION) {
        MSLLHOOKSTRUCT &msll = *(reinterpret_cast<MSLLHOOKSTRUCT*>(lParam));

        if (wParam == WM_RBUTTONDOWN) {
                cout << "OOO";
            if (WM_RBUTTONDOWN) {
                return -1; // Make this click be ignored
            }
        }
    }
    return CallNextHookEx(miHook, nCode, wParam, lParam);
}

int main() {
    system("pause");
}

Is this the right approach, or do I need to actually go into the registry to actually disable the right-click on the mouse?


Solution

  • Yes, you could disable mouse events by installing low level mouse hook. Your LowLevelMouseProc callback should return nonzero value if nCode is greater or equal zero. Also you should block not only WM_RBUTTONDOWN windows message, but WM_RBUTTONUP as well, or target application may work incorrectly.

    This code is pretty strange:

    if (wParam == WM_RBUTTONDOWN) {
                    cout << "OOO";
                if (WM_RBUTTONDOWN) {
                    return -1; // Make this click be ignored
                }
    

    Second if clause will always be true. Perhaps you mean

    if((WM_RBUTTONDOWN == wParam)||(WM_RBUTTONDOWN == wParam))
      return(-1);
    

    Also, callback is running in the context of your thread by processing a windows message, so your code must have a message queue, it could not be simple console application

    This hook is called in the context of the thread that installed it. The call is made by sending a message to the thread that installed the hook. Therefore, the thread that installed the hook must have a message loop.

    However system wide hooks could be pretty dangerous. One small error could easily make entire system unstable. Install hook only for the target application by specifying its message thread id in the SetWindowsHookEx call. Your other option would be subclassing target window by replacing its message processing routine and filtering mouse event messages.