Problem with QKeyEvent isAutoRepeat function?



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



  • @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();
    


  • 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?


  • Lifetime Qt Champion

    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



  • 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?



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



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.