Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. deactivate hook with qobject

deactivate hook with qobject

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 1.9k 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'm connecting to my external class with qobject to check mouse event's . But when i try to deactivate the hook with emit instance().event(); doesnt seem's to work the problem seem .this dont call signal. Obviously when i do this :

    mouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, 0);
    UnhookWindowsHookEx(mouseHook);
    

    Stop me the hook. But the signal never is called?¿
    Code:
    main.cpp:

    #include <QCoreApplication>
    #include <QDebug>
    #include "mouselogger.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QObject::connect(&MouseLogger::instance(), &MouseLogger::mouseEvent,
                         [](){
    
        });
    
        return a.exec();
    }
    
    

    mouse.cpp:

    #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";
        //}
    }
    
    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);
    }
    

    mouse.h:

    #ifndef MOUSELOGGER_H
    #define MOUSELOGGER_H
    
    #include <QObject>
    #include <windows.h>
    
    class MouseLogger : public QObject
    {
        Q_OBJECT
        Q_DISABLE_COPY(MouseLogger)
    public:
        static MouseLogger &instance();
        explicit MouseLogger(QObject *parent = nullptr);
        virtual ~MouseLogger(){}
    
        // Static method that will act as a callback-function
        static LRESULT CALLBACK mouseProc(int Code, WPARAM wParam, LPARAM lParam);
    signals:
        // The signal, which will report the occurrence of an event
        void mouseEvent();
        void Eventos();
    
    public slots:
    
    
    private:
        // hook handler
        HHOOK   mouseHook;
    };
    
    #endif // MOUSELOGGER_H
    
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      hi
      emit instance().event() ---> emit instance().Eventos();

      Would emit a signal.

      I dont see you have this signal hooked up to any slot ?

      J 1 Reply Last reply
      0
      • mrjjM mrjj

        hi
        emit instance().event() ---> emit instance().Eventos();

        Would emit a signal.

        I dont see you have this signal hooked up to any slot ?

        J Offline
        J Offline
        Jeronimo
        wrote on last edited by Jeronimo
        #3

        @mrjj yeah i didnt include because when i do this:
        QObject::connect(&MouseLogger::instance(), &MouseLogger::mouseEvent,
        {

        });
        

        The signal never is emitted. In other words , i want to call the signal but i think like you say me i need to include some slot first? to be emitted first.
        Ok i tried to do this creating slot define the slot:
        void MouseLogger::parada(){
        qDebug() << "test";
        emit instance().mouseEvent();

        }
        and call with:

        QObject::connect(&MouseLogger::instance(), &MouseLogger::mouseEvent,
        {

        });
        

        Not seem's to show nothing?

        mrjjM 1 Reply Last reply
        0
        • J Jeronimo

          @mrjj yeah i didnt include because when i do this:
          QObject::connect(&MouseLogger::instance(), &MouseLogger::mouseEvent,
          {

          });
          

          The signal never is emitted. In other words , i want to call the signal but i think like you say me i need to include some slot first? to be emitted first.
          Ok i tried to do this creating slot define the slot:
          void MouseLogger::parada(){
          qDebug() << "test";
          emit instance().mouseEvent();

          }
          and call with:

          QObject::connect(&MouseLogger::instance(), &MouseLogger::mouseEvent,
          {

          });
          

          Not seem's to show nothing?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Jeronimo

          Since its your signal. you defined it.
          You must also emit it.
          and if no slot is connected, nothing happens.

          So you did call
          MouseLogger::instance().parada() from main?

          J 1 Reply Last reply
          0
          • mrjjM mrjj

            @Jeronimo

            Since its your signal. you defined it.
            You must also emit it.
            and if no slot is connected, nothing happens.

            So you did call
            MouseLogger::instance().parada() from main?

            J Offline
            J Offline
            Jeronimo
            wrote on last edited by
            #5

            @mrjj i tried this but not seem's to work i'm trying to use qobject because the class is separated.
            But anyways not seem's to stop my hook:
            Main.cpp:

            #include <QCoreApplication>
            #include <QDebug>
            #include "mouselogger.h"
            
            int main(int argc, char *argv[])
            {
                QCoreApplication a(argc, argv);
            
                QObject::connect(&MouseLogger::instance(), &MouseLogger::mouseEvent,
                                 [](){
            
                });
                
            
                return a.exec();
            }
            
            

            mouse.cpp:

            #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(){
                mouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, 0);
                UnhookWindowsHookEx(mouseHook);
                emit instance().mouseEvent();
            }
            
            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;
            
                    }
            
            
                }
            
                // After that you need to return back to the chain hook event handlers
                return CallNextHookEx(NULL, Code, wParam, lParam);
            }
            

            mouse.h:

            #ifndef MOUSELOGGER_H
            #define MOUSELOGGER_H
            
            #include <QObject>
            #include <windows.h>
            
            class MouseLogger : public QObject
            {
                Q_OBJECT
                Q_DISABLE_COPY(MouseLogger)
            public:
                static MouseLogger &instance();
                explicit MouseLogger(QObject *parent = nullptr);
                virtual ~MouseLogger(){}
                HINSTANCE hInstance;
                void parada();
                // Static method that will act as a callback-function
                static LRESULT CALLBACK mouseProc(int Code, WPARAM wParam, LPARAM lParam);
            signals:
                // The signal, which will report the occurrence of an event
                void mouseEvent();
                void Eventos();
            
            public slots:
            
            
            private:
                // hook handler
                HHOOK   mouseHook;
            };
            
            #endif // MOUSELOGGER_H
            
            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              well you seems to create a new ONE and then stop that , not the one you create in constructor

              MouseLogger::MouseLogger(QObject *parent) : QObject(parent)
              {
              HINSTANCE hInstance = GetModuleHandle(NULL);
              //Set hook
              mouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, 0); <<< the FIRST

              void MouseLogger::parada(){
              mouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, 0); <<<< NEW ??
              UnhookWindowsHookEx(mouseHook);
              emit instance().mouseEvent();
              }

              so I would expect it to be

              void MouseLogger::parada(){
              UnhookWindowsHookEx(mouseHook);
              }

              J 1 Reply Last reply
              0
              • mrjjM mrjj

                well you seems to create a new ONE and then stop that , not the one you create in constructor

                MouseLogger::MouseLogger(QObject *parent) : QObject(parent)
                {
                HINSTANCE hInstance = GetModuleHandle(NULL);
                //Set hook
                mouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, 0); <<< the FIRST

                void MouseLogger::parada(){
                mouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, 0); <<<< NEW ??
                UnhookWindowsHookEx(mouseHook);
                emit instance().mouseEvent();
                }

                so I would expect it to be

                void MouseLogger::parada(){
                UnhookWindowsHookEx(mouseHook);
                }

                J Offline
                J Offline
                Jeronimo
                wrote on last edited by
                #7

                @mrjj ok thx one question i can call with this:
                MouseLogger::instance().parada();

                But then qobject can't be used to call parada(), only connect me the signal with the instance only so?

                mrjjM 1 Reply Last reply
                0
                • J Jeronimo

                  @mrjj ok thx one question i can call with this:
                  MouseLogger::instance().parada();

                  But then qobject can't be used to call parada(), only connect me the signal with the instance only so?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Jeronimo

                  Nothing should stop you from doing
                  MouseLogger::instance().parada(); directly

                  1 Reply Last reply
                  1

                  • Login

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