Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [SOLVED]Android home button
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Android home button

Scheduled Pinned Locked Moved Mobile and Embedded
7 Posts 3 Posters 7.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • niqtN Offline
    niqtN Offline
    niqt
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrdeeds
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • niqtN Offline
        niqtN Offline
        niqt
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • E Offline
          E Offline
          equalsraf
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrdeeds
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • E Offline
              E Offline
              equalsraf
              wrote on last edited by
              #6

              @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.

              1 Reply Last reply
              0
              • niqtN Offline
                niqtN Offline
                niqt
                wrote on last edited by
                #7

                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
                
                }
                

                @

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved