How to hook system events with QAbstractEventDispatcher



  • These are found in document
    EventFilter QAbstractEventDispatcher::setEventFilter ( EventFilter filter ):
    Replaces the event filter function for this QAbstractEventDispatcher with filter and returns the replaced event filter function.

    The event filter function set here is called for all messages taken from the system event loop before the event is dispatched to the respective target, including the messages not meant for Qt objects.

    The event filter function should return true if the message should be filtered, (i.e. stopped). It should return false to allow processing the message to continue.

    Is that mean we can catch all system events very easy?
    Then i tested it on windows.

    @#include <QAbstractEventDispatcher>
    #include <qt_windows.h>

    class SysEvent : public QObject
    {
    Q_OBJECT
    public:
    explicit SysEvent(QObject *parent = 0);
    ~SysEvent();

    protected:
    static bool sysEventFilter(void* message);
    static QAbstractEventDispatcher::EventFilter prevEventFilter;
    };

    SysEvent::SysEvent(QObject parent) :
    QObject(parent)
    {
    prevEventFilter = QAbstractEventDispatcher::instance()->setEventFilter(SysEvent::sysEventFilter);
    }
    bool SysEvent::sysEventFilter(void
    message)
    {
    qDebug("Event!");
    MSG* msg = static_cast<MSG*>(message);
    if (msg->message == WM_HOTKEY)
    {
    const quint32 keycode = HIWORD(msg->lParam);
    const quint32 modifiers = LOWORD(msg->lParam);
    qDebug("%d, %d", keycode, modifiers);
    return true;
    }
    return false;
    }
    @
    but when i run it, sysEventFilter() was called less then 10 times. I think other system event can call sysEventFilter() too. Am i wrong? What is the right way to catch system events with QAbstractEventDispatcher?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.