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

Not receiving keyboard events in QML



  • Hi All,
    I have written a small code for grabbing keyboard events in qml. Unfortunately I dont get any event in following code. Any suggestions please?
    (Also I dont get anything printed from onFocusChanged event)

    import QtQuick 2.7
    import QtQuick.Controls 2.1
    import QtQuick.Window 2.2

    Window {
    visible: true;
    width: Screen.width
    height: Screen.height

    Item  {
        id:keyLabelRect
        anchors.centerIn: parent
        height: 100
        width: 300
        focus: true
        Text {
            id:keyText
            anchors.centerIn: parent
            text:"Something"
            font.pointSize: 24; font.bold: true
        }
    
        onFocusChanged: {
            console.log("focus: ", focus)
        }
    
        Keys.enabled: true
        Keys.onPressed: {
            if(event.key === Qt.Key_F1) {
                keyText.text = "Key F1"
            }
            console.log(event.key)
        }
    }
    
    RoundButton {
        text: "X"
        height: 70
        width: 70
        onClicked: Qt.quit()
    }
    

    }



  • solved .....

    import QtQuick 2.7
    import QtQuick.Controls 2.1
    import QtQuick.Window 2.2

    Window {
    id:keyWindow
    visible: true;
    width: Screen.width
    height: Screen.height
    Component.onCompleted: keyWindow.requestActivate()

    Item  {
        id:keyLabelRect
        anchors.centerIn: parent
        height: 100
        width: 300
        focus: true
        Text {
            id:keyText
            anchors.centerIn: parent
            text:"Something"
            font.pointSize: 24; font.bold: true
        }
    
        onFocusChanged: {
            console.log("focus: ", focus)
        }
    
        Keys.enabled: true
        Keys.onPressed: {
            if(event.key === Qt.Key_F1) {
                keyText.text = "Key F1"
            }
            console.log(event.key)
        }
    }
    
    RoundButton {
        text: "X"
        height: 70
        width: 70
        onClicked: Qt.quit()
    }
    

    }


Log in to reply