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. Problem with QKeyEvent isAutoRepeat function?

Problem with QKeyEvent isAutoRepeat function?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qkeyeventqkeypresseventc++event
6 Posts 3 Posters 4.7k Views
  • 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.
  • F Offline
    F Offline
    faisal.tum
    wrote on 29 Jun 2016, 14:44 last edited by faisal.tum
    #1

    I have an application where if a particular button is kept pressed it plays an audio device, when the button is released it stops the audio device. I use keyPressEvent and keyReleaseEvent to implement this which is similar to the code below:

    void MainWindow::keyPressEvent(QKeyEvent *event)
    {
        if(event->isAutoRepeat())
        {
            event->ignore();
        }
        else
        {
            if(event->key() == Qt::Key_0)
            {
                qDebug()<<"key_0 pressed"<<endl;
            }
            else
            {
                QWidget::keyPressEvent(event);
            }
        }
    }
    
    void MainWindow::keyReleaseEvent(QKeyEvent *event)
    {
        if(event->isAutoRepeat())
        {
            event->ignore();
        }
        else
        {
            if(event->key() == Qt::Key_0)
            {
                qDebug()<<"key_0 released"<<endl;
            }
            else
            {
                QWidget::keyReleaseEvent(event);
            }
        }
    }
    

    But apparently isAutoRepeat function isn't working as I can see continuous print out of key_0 pressed and key_0 released one after another despite the fact I haven't released the 0 key after I have pressed it. Is my code wrong or something else is wrong?

    Thanks.

    K 1 Reply Last reply 29 Jun 2016, 15:14
    0
    • F faisal.tum
      29 Jun 2016, 14:44

      I have an application where if a particular button is kept pressed it plays an audio device, when the button is released it stops the audio device. I use keyPressEvent and keyReleaseEvent to implement this which is similar to the code below:

      void MainWindow::keyPressEvent(QKeyEvent *event)
      {
          if(event->isAutoRepeat())
          {
              event->ignore();
          }
          else
          {
              if(event->key() == Qt::Key_0)
              {
                  qDebug()<<"key_0 pressed"<<endl;
              }
              else
              {
                  QWidget::keyPressEvent(event);
              }
          }
      }
      
      void MainWindow::keyReleaseEvent(QKeyEvent *event)
      {
          if(event->isAutoRepeat())
          {
              event->ignore();
          }
          else
          {
              if(event->key() == Qt::Key_0)
              {
                  qDebug()<<"key_0 released"<<endl;
              }
              else
              {
                  QWidget::keyReleaseEvent(event);
              }
          }
      }
      

      But apparently isAutoRepeat function isn't working as I can see continuous print out of key_0 pressed and key_0 released one after another despite the fact I haven't released the 0 key after I have pressed it. Is my code wrong or something else is wrong?

      Thanks.

      K Offline
      K Offline
      koronabora
      wrote on 29 Jun 2016, 15:14 last edited by koronabora
      #2

      @faisal.tum said:

      QKeyEvent

      Your code works well for me:

      key_0 pressed 
      
      key_0 released 
      
      

      Chtck your keyboard.

      QApplication a(argc, argv);
      	QtTestClient w;
      	QObject::connect(&w, &QtTestClient::keyPressEvent, &w, &QtTestClient::keyPressEvent);
      	QObject::connect(&w, &QtTestClient::keyReleaseEvent, &w, &QtTestClient::keyReleaseEvent);
      	w.show();
      	return a.exec();
      
      1 Reply Last reply
      0
      • F Offline
        F Offline
        faisal.tum
        wrote on 30 Jun 2016, 07:58 last edited by faisal.tum
        #3

        I think this is happening because the MainWindow loses the keyboard focus. How can I actually find out which widget has the focus? I'm actually using some widgets when Qt::Key_0 pressed, but I thought I set all those possible widgets to Qt::NoFocus, I guess it's not working.

        I'm trying to know which widget has the focus by doing the following:

        QWidget * wigdet = QApplication::activeWindow();
        qDebug()<<wigdet->accessibleName()<<endl;

        but it always prints an empty string. How can I make it print the name of the widget which has the keyboard focus?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 30 Jun 2016, 08:01 last edited by
          #4

          Unless you set object name, it dont have one.
          you can use
          http://doc.qt.io/qt-4.8/qmetaobject.html#className
          to know the class type

          1 Reply Last reply
          0
          • F Offline
            F Offline
            faisal.tum
            wrote on 30 Jun 2016, 09:22 last edited by faisal.tum
            #5

            Anyway, found an interesting situation. When my mouse is over the window, it produces keyReleaseEvent despite the key being pressed, but when the mouse is outside the window, it behaves as expected, that is it doesn't produce keyReleaseEvent.

            One of the widgets is of the type:

            
            class ArWidget : public QGLWidget, public QOpenGLFunctions_4_3_Core, public KinectProcessorFusion
            {
                Q_OBJECT
            
            public:
                explicit ArWidget(QWidget *parent);
                virtual ~ArWidget();
            
                void setCalibration(CalibrationData &data1);
            
                void setTransformations(const Eigen::Matrix4f &mat1, const Eigen::Matrix4f &mat2);
            
            public slots:
                virtual void process(ProcessorContainer &frames1, ProcessorContainer &frames2);
                void clearContent();
                void setKinect(int index);
                void imageRotation(bool rotate);
            
            signals:
                void mouseClicked(float x, float y);
                void widgetSizeChanged();
            
            protected:
                enum StreamType
                {
                    VIDEO,
                    EDGE,
                    DEPTH
                };
            
                virtual void initializeGL();
                virtual void paintGL();
                virtual void resizeGL(int width, int height);
                virtual void wheelEvent(QWheelEvent *event);
            
                virtual void mouseMoveEvent(QMouseEvent *event);
                virtual void mousePressEvent(QMouseEvent *event);
            
                void uploadStreamTexture(StreamType type, const cv::Mat &source);
                void renderStreamTexture();
                void renderPointCloud();
            }
            

            This widget shows the image from kinect and allows mouse interactions like mouseMoveEvent, mousePressEvent, wheelEvent on it. When I delete this widget from my window through the QForm in QtCreator, I can see QKeyEvent working perfect. It seems this particular widget takes away the keyboard focus immediately after the Key_0 is pressed and produces a keyReleaseEvent for Key_0.

            Any suggestion how to keep the keyboard focus intact on the MainWindow class?

            1 Reply Last reply
            0
            • F Offline
              F Offline
              faisal.tum
              wrote on 30 Jun 2016, 11:37 last edited by faisal.tum
              #6

              Anyway solved it. I needed to call grabKeyboard function from the MainWindow class, so this->grabKeyboard() is the line I needed to add when key_0 button is pressed so that MainWindow doesn't lose the keyboard focus, and then when released I needed to add the line this->releaseKeyboard() to resume normal behaviour, that is, other widgets can have the keyboard focus.

              1 Reply Last reply
              0

              6/6

              30 Jun 2016, 11:37

              • Login

              • Login or register to search.
              6 out of 6
              • First post
                6/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved