Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. Mouse program with windows hook
Forum Updated to NodeBB v4.3 + New Features

Mouse program with windows hook

Scheduled Pinned Locked Moved 3rd Party Software
1 Posts 1 Posters 830 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jeronimo
    wrote on last edited by Jeronimo
    #1

    Hi i am searching info because i'm improving one mouseevent program. At the momen't could unistall with:
    UnhookWindowsHookEx(hHook);

    But if i want that the event's work again, how i can install again? So the problem is when i use this:

    MouseLogger::instance().parada();
    

    And i connect again, the mouse program doesnt work more,like:

    QObject::connect(&MouseLogger::instance(), &MouseLogger::mouseEvent,
                         [](){
    
        });
    

    Program:

    #include "mouselogger.h"
    #include <QDebug>
    
    MouseLogger &MouseLogger::instance()
    {
        static MouseLogger _instance;
        return _instance;
    }
    
    MouseLogger::MouseLogger(QObject *parent) : QObject(parent)
    {
        HINSTANCE hInstance = GetModuleHandle(NULL);
         //Set hook
        mouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, 0);
        // Check hook is correctly
        if (mouseHook == NULL) {
            qWarning() << "Mouse Hook failed";
        }
    }
    
    void MouseLogger::parada(){
        UnhookWindowsHookEx(mouseHook);
    }
    
    LRESULT CALLBACK MouseLogger::mouseProc(int Code, WPARAM wParam, LPARAM lParam)
    {
        Q_UNUSED(Code)
    
        // Having an event hook, we nned to cast argument lParam
        // to the structure of the mouse is the hook.
        MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
    
        // Next, we check to see what kind of event occurred,
        // if the structure is not a pointer to nullptr
        if(pMouseStruct != nullptr) {
            switch (wParam) {
            case WM_MOUSEMOVE:
                qDebug() << "WM_MOUSEMOVE";
                break;
            case WM_LBUTTONDOWN:
                qDebug() << "WM_LBUTTONDOWN";
                break;
            case WM_LBUTTONUP:
                qDebug() << "WM_LBUTTONUP";
                break;
            case WM_RBUTTONDOWN:
                qDebug() << "WM_RBUTTONDOWN";
                break;
            case WM_RBUTTONUP:
                qDebug() << "WM_RBUTTONUP";
                break;
            case WM_MBUTTONDOWN:
                qDebug() << "WM_MBUTTONDOWN";
                break;
            case WM_MBUTTONUP:
                qDebug() << "WM_MBUTTONUP";
                break;
            case WM_MOUSEWHEEL:
                qDebug() << "WM_MOUSEWHEEL";
                break;
            default:
                break;
             emit instance().mouseEvent();
            }
    
    
        }
    
        // After that you need to return back to the chain hook event handlers
        return CallNextHookEx(NULL, Code, wParam, lParam);
    }
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved