[SOLVED]Android home button



  • Hi,
    i need to capture of the event HOME key press (or realase):
    i try so:
    @
    Keys.onPressed: {

            if (event.key === Qt.Key_Home) {
                console.log("home")
                event.accepted = true
            }
        }
    

    @

    but nothing. I need of this function beacuse i must stop music of application.
    Has someone the solution?



  • Hi,
    I have looked into Android hardware button events a few times for my own application development. From what I have seen the Home button event (along with the App Switch button event) is consumed by the device and so not passed to the application.
    The back button event can however be seen by the application and custom handling implemented. This is likely to conform with the Andorid standard use of these buttons.
    I would suggest looking for other ways to identfy when the application has been minimised. I imagine something to do with Qt::WindowState would provide what you want.



  • Hi,
    I've already tried Qt :: WindowState and ChangeFocus, but nothing.



  • Hi,

    I'm having similar issues, I can't get events for the menu key (but back works ok).

    If you are in a hurry the best bet is probably to write a bit of Java to override the activity - check the qtandroidextras "notification example":http://qt-project.org/doc/qt-5/qtandroidextras-notification-example.html

    To know if the activity is in foreground/background you can track the state using the onResume/onPause() methods.



  • This is for the same reason as I outlined earlier. The Home and Menu button are not designed to be processed by applications. They are buttons that are supposed to have one set operation that the user can depend upon. This is why the key events for these buttons are not passed to the application. If they could be reimplemented by the developer applications could prevent the user from closing them.
    In a standard Android application these buttons trigger the onPause()/onResume() methods as you stated and this is where the required processing is done.
    Trying to implement code on these button presses is the wrong approach, but like the standard Android applications there are ways to determine when the application is minimised. Through monitoring these you can run code when the application is closed/minmised.
    If Qt::WindowState does not work try looking into Qt::ApplicationState
    The back button events can be caught by the application as it is designed to be a method of closing or going back through the application.



  • @mrdeeds thanks for detailed explanation and the heads up on Qt::ApplicationState

    I just did a quick check on Android and QGuiApplication::applicationState() seems to return Qt::ApplicationSuspended when the app is in the background and Qt::ApplicationActive when in the foreground, which was exactly what I was looking for.



  • My example that implements mrdeeds tip:

    @
    ApplicationWindow {

    id: window
    
    property bool mute: false
    
    onMuteChanged: {
    
        if (mute) {
            playMusic.stop()
        } else {
            playMusic.play();
        }
    }
    

    states: [
    State {
    name: "mut"; when: Qt.application.state !== Qt.ApplicationActive
    PropertyChanges {
    target: window;
    mute: true
    }
    }
    ]
    }

    MediaPlayer {
        id: playMusic
        volume: 0.5
        source: "my.mp3"
        autoPlay: true
    
    }
    

    @


Log in to reply
 

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