Advertisement
Ilya_Bykonya

Untitled

Mar 22nd, 2024
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. class CustomKeyCatcher: public QThread {
  3. public:
  4.     explicit CustomKeyCatcher(QObject* parent = nullptr)
  5.         :QThread{ parent } {}
  6.     void interruptAndWait() {
  7.         requestInterruption();
  8.         wait();
  9.     }
  10. protected:
  11.     virtual void run() override {
  12.         while(not isInterruptionRequested()) {
  13.             const auto symbol = getchar();
  14.             std::cout << "Key pressed: " << symbol << std::endl;
  15.         }
  16.     }
  17. };
  18.  
  19. int main(int argc, char** argv) {
  20.     QCoreApplication app{ argc, argv };
  21.  
  22.     auto catcher = new CustomKeyCatcher{ &app };
  23.     QObject::connect(&app, &QCoreApplication::aboutToQuit, catcher, &CustomKeyCatcher::interruptAndWait);
  24.     catcher->start(QThread::Priority::LowestPriority);
  25.  
  26.     return app.exec();
  27. }
  28.  
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement