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

qml : Keys.onPressed does not work but Keys.onReleased works fine inside FocusScope.Please help me in fixing the issue.



  • Hey everyone, I'm having an issue with keyPress events in lListView written inside FocusScope
    Keys.onReleased works but Keys.onPressed does not work.
    In fact, not even Keys.onUpPressed is not working. Please help me in fixing this.

    Thanks in advance.


  • Moderators

    @Sudo007
    show some code pls



  • import QtQuick 2.2
    
    FocusScope {
        id:mainPage
        
        signal somethingWasClicked(var sender, var arguments)
    
        function createSpyFunction(object) {
            return function() { somethingWasClicked(object, arguments) }
        }
    
        function crawlChildren(obj) {
            if (obj.Keys.onPressed) {
                Object.defineProperty(obj, '__clickedFunction', { value: createSpyFunction(obj) })
                obj.Keys.onPressed.connect(obj.__clickedFunction)
            }
    
            if (obj.children) {
                for (var i = 0; i < obj.children.length; i++) {
                    crawlChildren(obj.children[i])
                }
            }
        }
    	
        Component.onCompleted: {
            mainPage.crawlChildren(screenBG)
        }
    
        Rectangle{
            id: screenBG
            anchors.fill: parent
    
         
      Keys.onPressed: {
    
            console.log("#onPressed#")
    
            switch(event.key)
            {
                case Qt.Key_Up:// not working
                    console.log('onPressed##Key_Up was pressed')
                    break
                case Qt.Key_Down: // only down key event is triggred 
                     console.log('onPressed##Key_Down was pressed')
                     break
            }
         }
        }  
        Rectangle {
            z: 99999
            anchors.fill: parent
            color: "black"
        }
    }
    
    
    // There is child screen ( qml page having List View ). for which down key event works but not up key
    
    //
        // Display the configured users as a vertical list of 'menubutton' items, one per user.
        // /// usr.qml
        ListView{
            id: userList
            width: parent.width
            anchors.top: usersCountBar.bottom
            anchors.bottom: parent.bottom
    
            focus: true
            clip: true
        // ..
        }
    

Log in to reply