Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CustomKeyCatcher: public QThread {
- public:
- explicit CustomKeyCatcher(QObject* parent = nullptr)
- :QThread{ parent } {}
- void interruptAndWait() {
- requestInterruption();
- wait();
- }
- protected:
- virtual void run() override {
- while(not isInterruptionRequested()) {
- const auto symbol = getchar();
- std::cout << "Key pressed: " << symbol << std::endl;
- }
- }
- };
- int main(int argc, char** argv) {
- QCoreApplication app{ argc, argv };
- auto catcher = new CustomKeyCatcher{ &app };
- QObject::connect(&app, &QCoreApplication::aboutToQuit, catcher, &CustomKeyCatcher::interruptAndWait);
- catcher->start(QThread::Priority::LowestPriority);
- return app.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement