leoroop
leoroop

Reputation: 11

Keyboard events for Keys.onPressed not handled over some conditions

I'm trying to get the button event from a USB camera on an application running under linux (custom built using Yocto Project) on an embedded system. Currently I'm using Qt 5.6.3. My problem is that the code I show right below works like a charm while I run the code through SSH from my pc (both via Qt Creator and a simple shell), but if I run the same program directly on the system without using SSH nothing happens when i click the button on the camera (nor any other key from a keyboard really).

Following some examples online I used Keys.onPressed and then filter the event to get the desired behaviour. To get it globally I put the event handler inside an Item directly in my ApplicationWindow.

ApplicationWindow {
    Item {
        focus: true
        Keys.onPressed:{
            console.log(event.key)
            playSound.play() //I play a sound to be sure a button is clicked
            if(camera.recording && event.key === 16777466) {
            //do stuff for the right key here            
            }
        }
    }
    //Other elements here
}

I think it has something to do with the X server running on my system. But everything is default and I really don't know what to look for in my system to get a hint of what's not working. Any advice il really appreciated.

Upvotes: 1

Views: 1273

Answers (3)

leoroop
leoroop

Reputation: 11

After some hours of searching it turned out it was really a problem with the X server's "active window", the solution was very simple though, I just had to add requestActivate(); on my main view just like this:

ApplicationWindow {
    Item {
        focus: true
        Keys.onPressed:{
            console.log(event.key)
            playSound.play() //I play a sound to be sure a button is clicked
            if(camera.recording && event.key === 16777466) {
            //do stuff for the right key here            
            }
        }
    }

    StackView{
        id: rootView
        width: 1280
        height: 800
        Component.onCompleted: {
            requestActivate(); //THIS SOLVED THE PROBLEM
            push(mainarea);
        }
    //Other elements here
}

Upvotes: 0

Adriano Campos
Adriano Campos

Reputation: 1199

Maybe a problem related with Focus. What about forcing the focus after onCompleted event?

ApplicationWindow {
    Item {
        id: myItem
        focus: true
        Keys.onPressed:{
            console.log(event.key)
            playSound.play() //I play a sound to be sure a button is clicked
            if(camera.recording && event.key === 16777466) {
            //do stuff for the right key here
            }
        }
        Component.onCompleted: {
            myItem.forceActiveFocus()
        }
    }
    Component.onCompleted: {
        myItem.forceActiveFocus()
    }
    //Other elements here
}

Upvotes: 1

simona
simona

Reputation: 15

can you prove to set width and height of item?

Upvotes: 0

Related Questions