Unsolved QCoreApplication to read keyboard
-
I am using Ubuntu 16.04.3 and just upgraded to QT5.10. I would like to read keyboard input during boot so there will not be a window or user interface, just typing in blind. I have the following code:
mount.hpp
#include <QCoreApplication> #include <QObject> #include <QThread> class KeyBrdRdr: public QThread { Q_OBJECT public: KeyBrdRdr(void); ~KeyBrdRdr(void); void run(); signals: void KeyPressed(char); }; class KeyBrdHndlr: public QObject { Q_OBJECT public: KeyBrdHndlr(void); ~KeyBrdHndlr(void); QString strString; quint16 uiAttempts; KeyBrdRdr *kbUser; public slots: void OnKeyPressed(char); };
main.cpp
#include <QCoreApplication> #include <QObject> #include <QThread> #include <QFile> #include <QTextStream> #include "mount.hpp" #include <termios.h> #include <unistd.h> #include <stdio.h> static struct termios oldSet, newSet; KeyBrdRdr::KeyBrdRdr(void) { tcgetattr( STDIN_FILENO, &oldSet ); newSet = oldSet; newSet.c_lflag &= ~( ICANON | ECHO ); tcsetattr( STDIN_FILENO, TCSANOW, &newSet ); } KeyBrdRdr::~KeyBrdRdr(void) { tcsetattr( STDIN_FILENO, TCSANOW, &oldSet ); } void KeyBrdRdr::run() { while (true) { char key = getchar(); emit KeyPressed(key); } return; } KeyBrdHndlr::KeyBrdHndlr(void) { strString = ""; uiAttempts = 3; kbUser = new KeyBrdRdr(); QObject::connect (kbUser, SIGNAL (KeyPressed(char)), this, SLOT(OnKeyPressed(char))); } KeyBrdHndlr::~KeyBrdHndlr(void) { kbUser->exit(); } void KeyBrdHndlr::OnKeyPressed(char cCurrent) { if (cCurrent == 10 ) // enter { if (strString == "lmnop") { // start process QCoreApplication::exit(0); } else { if (uiAttempts == 0) QCoreApplication::exit(1); uiAttempts--; strString.truncate(0); } // for testing QFile file("/home/user/test.txt"); if (file.open(QIODevice::Append)) { QTextStream stream(&file); stream << strString << "\n\r"; file.close(); } } else strString.append(cCurrent); return; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); KeyBrdHndlr *kbCheck = new KeyBrdHndlr(); kbCheck->kbUser->start(); return a.exec(); }
The thread seems to initialize and start properly. But past that point the debugger will not interrupt at a break point in OnKeyPressed on a key press. I added the code to write to a text file, which remains empty. Any suggestions would be greatly appreciated...
-
Hi @CrazyDave,
Not entirely sure about all the logic etc, but it worked for me.
I used your code above verbatim, with this quick-and-dirty
test.pro
file:CONFIG += console HEADERS += mount.hpp SOURCES += main.cpp
Perhaps you forgot
CONFIG += console
?All I tested was that after typing
lmnop
it exits, and that the\r\n
andlmop
is being appended to the file exactly as I'd expect based on reading the code. Note, as you are truncatingstrString
where you do, no other input makes it to the test file, but I don't think that matters, since the file was just to prove that its executing.Oh, and also note it left my console in a funky state (easily fixed). Probably need to restore the flags on exit.
Cheers.
-
@Paul-Colby
Thanks Paul, I was trying to user without a console. I am now trying to use the event files with read. -
@CrazyDave so with Paul's suggestion, it looks like your problem is solved. If so, please don't forget to mark your post as such. Thanks