#StackBounty: #c++ #qt #keyboard #mouseevent #keypress How to capture keyboard & mouse events even when the Qt app is minimized / i…

Bounty: 50

Here the requirement is just to check if any key was pressed or any mouse click or movement has happened. Capturing other specific details is not required.
I am fine to do every 1 second polling as well to see if any mouse or key event happened.

Will QAbstractNativeEventFilter be of any help?
Any other platform independent C++ library also will be useful.


Below is a sample code to capture mouse & keyboard events only when the app is in focus:

#include<QApplication>
#include<QDebug>
#include<QKeyEvent>
#include<QWidget>

struct Widget : public QWidget
{
  Widget ()
  {
    installEventFilter(this);
    grabKeyboard();
    grabMouse();
    setMouseTracking(true);
  }
  ~Widget () { qDebug() << "~Event()"; }

  bool eventFilter (QObject* const pObject,
                    QEvent* const pEvent) override
  {
    qDebug() << "Event: " << pEvent->type();
    if(pEvent->type() == QEvent::KeyPress)
    {
      QKeyEvent* const pKeyEvent = static_cast<QKeyEvent*>(pEvent);
      qDebug() << "Key event: " << pKeyEvent->key();
    }
    return false; //QObject::eventFilter(pObject, pEvent);
  }
};


int main (int argc, char *argv[])
{
  QApplication application(argc, argv);
  Widget widget;
  widget.show();
  return application.exec();
}


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.