Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Get physical key events



  • QT application is developed based on Embedded Linux. The device is non touch screen, using Weston. Press the button to report the event normally, but the application can't get the button event. Which direction should we start to solve this situation?

    Thanks!


  • Qt Champions 2019

    @xixi_cly said in Get physical key events:

    Press the button

    What button? Keyboard? Mouse button?



  • @jsulm
    the phone key ,like the Nokia phone keys



  • @jsulm
    I have used the following procedure to test:

    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        setFocusPolicy(Qt::StrongFocus);
        setFixedSize(800, 800);
        qDebug()<<"begin test"<<endl;
        btn = new QPushButton(tr("open"),this);
        if (hasFocus()) {
            qDebug() << "has focus" << endl;
        } else {
            qDebug() << "NO focus" << endl;
        }
    }
    
    Widget::~Widget()
    {
    }
    
    void Widget::keyPressEvent(QKeyEvent *event) {
        qDebug()<<"try"<<endl;
        if(event->key() == Qt::Key_1) {
            qDebug()<<"press 1"<<endl;
            btn->move(50, 50);
            
            if (hasFocus()) {
                qDebug() << "Get focus" << endl;
            } else {
                qDebug() << "NO focus" << endl;
            }
        }
    }
    

    But when I press the key,nothing happend. It seems that the application doesn't get the focus.


  • Moderators

    @xixi_cly the application gets the focus, I'm sure about it,

    try either overriding the top most QWidget keypress event or - the probably better option - install an event filter on your application



  • I second the idea (from @J-Hilk ) to try installEventFilter.

    Another (somewhat) common Qt debugging tactic is to use QT_LOGGING_RULES, but it isn't always clear to me which logging categories to enable.

    In your case, doing this in the environment before app launch might produce useful logs:

    export QT_LOGGING_RULES="qt.qpa.input* = true"
    

Log in to reply