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. QCoreApplication to read keyboard
Forum Updated to NodeBB v4.3 + New Features

QCoreApplication to read keyboard

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 3.5k Views 2 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.
  • C Offline
    C Offline
    CrazyDave
    wrote on last edited by
    #1

    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...

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by Paul Colby
      #2

      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 and lmop is being appended to the file exactly as I'd expect based on reading the code. Note, as you are truncating strString 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.

      C 1 Reply Last reply
      2
      • Paul ColbyP Paul Colby

        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 and lmop is being appended to the file exactly as I'd expect based on reading the code. Note, as you are truncating strString 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.

        C Offline
        C Offline
        CrazyDave
        wrote on last edited by
        #3

        @Paul-Colby
        Thanks Paul, I was trying to user without a console. I am now trying to use the event files with read.

        1 Reply Last reply
        0
        • Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @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

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          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