Qt WinAPI - Lesson 009. SetWindowsHookEx - Logging mouse events via WinAPI

Hook, mouse, Qt, SetWindowsHookEx, WinAPI

Functional WinAPI allows by using hooks to monitor system events, such as mouse movement and clicks. This functionality based on callback functions, so if you want to use the Qt system of signaling and slots , you will need to pass one of the methods as a callback to a function for registering callback on a Windows system. But the method must be static, so you need to develop the class as a Singleton.

SetWindowsHookEx

This function is used to register an event handler in the chain hook-handlers to monitor certain events in Windows

HHOOK WINAPI SetWindowsHookEx(
  _In_ int       idHook,
  _In_ HOOKPROC  lpfn,
  _In_ HINSTANCE hMod,
  _In_ DWORD     dwThreadId
);

Parameters

idHook [in]

Type: int

The type of hook procedure to be installed. This parameter can be one of the following values.

  • WH_CALLWNDPROC - Installs a hook procedure that monitors messages before the system sends them to the destination window procedure.
  • WH_CALLWNDPROCRET - Installs a hook procedure that monitors messages after they have been processed by the destination window procedure.
  • WH_CBT - Installs a hook procedure that receives notifications useful to a CBT application.
  • WH_DEBUG - Installs a hook procedure useful for debugging other hook procedures.
  • WH_FOREGROUNDIDLE - Installs a hook procedure that will be called when the application's foreground thread is about to become idle. This hook is useful for performing low priority tasks during idle time.
  • WH_GETMESSAGE -Installs a hook procedure that monitors messages posted to a message queue.
  • WH_JOURNALPLAYBACK - Installs a hook procedure that posts messages previously recorded by a WH_JOURNALRECORD hook procedure.
  • WH_JOURNALRECORD - Installs a hook procedure that records input messages posted to the system message queue. This hook is useful for recording macros.
  • WH_KEYBOARD - Installs a hook procedure that monitors keystroke messages.
  • WH_KEYBOARD_LL - Installs a hook procedure that monitors low-level keyboard input events.
  • WH_MOUSE - Installs a hook procedure that monitors mouse messages.
  • WH_MOUSE_LL - Installs a hook procedure that monitors low-level mouse input events.
  • WH_MSGFILTER - Installs a hook procedure that monitors messages generated as a result of an input event in a dialog box, message box, menu, or scroll bar.
  • WH_SHELL - Installs a hook procedure that receives notifications useful to shell applications.
  • WH_SYSMSGFILTER - Installs a hook procedure that monitors messages generated as a result of an input event in a dialog box, message box, menu, or scroll bar. The hook procedure monitors these messages for all applications in the same desktop as the calling thread.

lpfn [in]

Type: HOOKPROC

A pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a DLL. Otherwise, lpfn can point to a hook procedure in the code associated with the current process.

hMod [in]

Type: HINSTANCE

A handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.

dwThreadId [in]

Type: DWORD

The identifier of the thread with which the hook procedure is to be associated. For desktop apps, if this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread. For Windows Store apps, see the Remarks section.

Return value

Type: HHOOK

If the function succeeds, the return value is the handle to the hook procedure. If the function fails, the return value is NULL . To get extended error information, call GetLastError .

Project Structure

After a brief introduction to the feature you will use to set the hook, look at the structure of the project, which will demonstrate the connectivity option function to tracking the mouse.

  • MouseHook.pro - project profile;
  • main.cpp - file with main function;
  • mouselogger.h - header file of class for mouse event logging;
  • mouselogger.cpp - source file of class for mouse event logging.

MouseHook.pro

QT += core
QT -= gui

CONFIG += c++11

TARGET = MouseHook
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp \
    mouselogger.cpp

HEADERS += \
    mouselogger.h

main.cpp

In this file, to demonstrate the operation of signals and slots, shows the connection of the lambda functions to the object for logging mouse events, but applied to the connection object using the pattern "Singleton".

#include <QCoreApplication>
#include <QDebug>
#include "mouselogger.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QObject::connect(&MouseLogger::instance(), &MouseLogger::mouseEvent,
                     [](){
        qDebug() << "Mouse Event";
    });

    return a.exec();
}

mouselogger.h

Header file of class for logging messages about mouse events. In this file, you must declare static methods:

  1. To return an object reference singleton through which slot has been connected to the signal of the mouse event.
  2. Static method mouseProc, which will be used for event handling.
#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();

public slots:

private:
    // hook handler
    HHOOK   mouseHook;
};

#endif // MOUSELOGGER_H

mouselogger.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);
}

Result

The result will be to monitor the mouse events, regardless of whether the mouse cursor is in the focus of the program, if the application has a graphical user interface, or is not in the program focus.

Video

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.
Support the author Donate

Comments

Only authorized users can post comments.
Please, Log in or Sign up
Donate

Hello, Dear Users of EVILEG!!!

If the site helped you, then support the development of the site financially, please.

You can do it by following ways:

Thank you, Evgenii Legotckoi

LP
Nov. 12, 2019, 8:22 a.m.
Lev Parhimovich

C++ - Test 006. Enumerations

  • Result:50points,
  • Rating points-4
LP
Nov. 12, 2019, 7:35 a.m.
Lev Parhimovich

C++ - Test 005. Structures and Classes

  • Result:66points,
  • Rating points-1
LP
Nov. 12, 2019, 7:26 a.m.
Lev Parhimovich

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:50points,
  • Rating points-4
Last comments
b
Nov. 9, 2019, 8:28 a.m.
bastonc

спасибо ещё раз. огромное, за уделённое время
b
Nov. 9, 2019, 8:24 a.m.
bastonc

Спасибо Вам большое. Буду изучать.
Nov. 9, 2019, 5:58 a.m.
Evgenij Legotskoj

Добрый день. По первым двум вопросам вы найдёте ответ в этой статье - PyQt5 - Урок 008. Работа с QTableWidget (Обновление урока 006) Что касается последнего вопроса, то я вам…
Nov. 9, 2019, 2:50 a.m.
Evgenij Legotskoj

Как и обещал, вы можете посмотреть новую статью QML - Урок 037. Кастомизация кнопок в QML (Обновление урока 002) . Там же найдёте ссылку на Git репозиторий. Не забудьте поставить звёз…
b
Nov. 8, 2019, 7:40 a.m.
bastonc

Приветствую. Подскажите пожалуйста пару моментов. 1. Как сделать столбец не редактируемый, а остальные ячейки остаются редактируемыми 2. Как оталвливать события двойного клика для реда…
Now discuss on the forum
KZ
Nov. 13, 2019, 10:07 a.m.
Konstantin Znamenskii

Добрый день. Я хочу узнать, как наиболее грамотно обновлять Maintenance Tool у пользователя. Я периодически выкладываю обновления пакетов самой программы, но мне также нужно вносить изменения в …
Nov. 13, 2019, 9:33 a.m.
Pavel.K

Приложение трэкинг задач. Есть вложения. Добавляем вложение и отправляем его на сервер. Для синхронного вызова методов в синхронизации, используем QEventLoop при отправке вложений. В момент син…
Nov. 13, 2019, 6:41 a.m.
Ruslan Polupan

Ну ка кбы уже лет 10 как работает :-) Просто нужна отдельная прога для добавления таких записей, отдать клиентам чтобы мозг не парили....
Nov. 13, 2019, 3:13 a.m.
Evgenij Legotskoj

Добрый день. Думаю, что да. Выбранный стиль можно подгружать при запуске программы. Во всяком случае, есть такой пример на C++ - Controls Gallery . И там есть такой код #incl…
Nov. 12, 2019, 9:35 a.m.
Evgenij Legotskoj

Добрый день. Для корректной вставки ссылок на youtube вставляйте их через пустую строку, таким образом Первая ссылка Вторая ссылкаТретья ссылка
EVILEG
About
Services
© EVILEG 2015-2019
Recommend hosting TIMEWEB