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. How to hook system events with QAbstractEventDispatcher
Forum Updated to NodeBB v4.3 + New Features

How to hook system events with QAbstractEventDispatcher

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 2.9k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    ibingow
    wrote on last edited by
    #1

    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?

    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