Search code examples
winapiwin32gui

How to disable mouse scroll wheel button permanently


I have a question that's bothering me. So I have a friend whose mouse scroll wheel just broke, but not mechanically, it's just went crazy - scrolling by itself without any input. I wrote a simple c++/batch program, which asks you if you wanna disable scroll button by modifying values in registry:

reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v "WheelScrollChars" /t REG_SZ /d 0 /f
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v "WheelScrollLines" /t REG_SZ /d 0 /f

Unfortunately, it works only in some applications e.g. chrome. Like when he opens Adobe Premiere or any additional software, the scroll actually is not disabled - it keeps scrolling by itself randomly up and down. Yeah, I know that's not a problem to buy a new mouse, but I'm just curious if there's any other way around it. And I found a piece of software called X-Mouse Button Control (written in C/C++ I guess), which actually have an option to disable scrolling permanently but it only works while program is running. I tried to reverse its binary in IDA although couldn't find actual function used to disable scrolling in whole system, not just couple of Windows apps.

So I thought I'd ask, maybe you have some ideas if there are possibilities in C++ to make it disabled in whole Windows (within any application running)? I guess if someone did it via this software, X-Mouse Button Control, it's probably possible but I haven't found anything yet. Anyway, any help is much appreciated and I'm looking forward to your replies, thanks!


Solution

  • The method has been given in the comments. All I have done is to familiarize you with the use of hook.

    The following is the most concise implementation code for your reference:

    #include <Windows.h>
    #include <iostream>
    
    using namespace std;
    
    HHOOK mouseHook;
    
    LRESULT __stdcall MouseHookCallback(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if (nCode >= 0)
        {
            switch (wParam)
            {       
            case WM_MOUSEWHEEL:
                return 1;
            }
        }
        return CallNextHookEx(mouseHook, nCode, wParam, lParam);
    }
    
    void SetHook()
    {
        if (!(mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookCallback, NULL, 0)))
        {
            cout << "Failed to install mouse hook!" << endl;
        }
    }
    
    void ReleaseHook()
    {
        UnhookWindowsHookEx(mouseHook);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        SetHook();
        MSG msg;
    
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return msg.wParam;
    }
    

    The mouse wheel is intercepted by the mouse hook, so that the mouse wheel is disabled.