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. Handling Android volume key

Handling Android volume key

Scheduled Pinned Locked Moved Solved Mobile and Embedded
11 Posts 4 Posters 4.4k Views
  • 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.
  • SeeLookS Offline
    SeeLookS Offline
    SeeLook
    wrote on last edited by
    #1

    Hi!

    Volume keys are not handled by QKeyEvent, so how to capture them?

    D 1 Reply Last reply
    0
    • SeeLookS SeeLook

      Hi!

      Volume keys are not handled by QKeyEvent, so how to capture them?

      D Offline
      D Offline
      Devopia53
      wrote on last edited by
      #2

      @SeeLook

      Hi.

      You wanna get the event where? cpp or java?

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Do you mean on some keyboards there is Volume keys to adjust sound level?
        Those are often handled by keyboard driver. If you mean on android I guess its
        another story.

        You should at least specify platform and Qt version.

        1 Reply Last reply
        1
        • SeeLookS Offline
          SeeLookS Offline
          SeeLook
          wrote on last edited by
          #4

          @Devopia53
          I would like to have c++ code after vol key was pressed
          @mrjj
          Of course I meant Android with Qt starting from 5.6 versions.

          I got some hint here:
          http://lists.qt-project.org/pipermail/android-development/2016-October/000889.html

          When I deploy that I will write also here, but if You know more direct example, please let me know.

          D 1 Reply Last reply
          0
          • SeeLookS SeeLook

            @Devopia53
            I would like to have c++ code after vol key was pressed
            @mrjj
            Of course I meant Android with Qt starting from 5.6 versions.

            I got some hint here:
            http://lists.qt-project.org/pipermail/android-development/2016-October/000889.html

            When I deploy that I will write also here, but if You know more direct example, please let me know.

            D Offline
            D Offline
            Devopia53
            wrote on last edited by Devopia53
            #5

            @SeeLook

            Ok! very simple! :)

            // in your main.cpp
            
            [...]
            int main(int argc, char *argv[])
            {
                qputenv("QT_ANDROID_VOLUME_KEYS", "1"); // "1" is dummy
                QApplication a(argc, argv);
            
            [...]
            }
            
            1 Reply Last reply
            3
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              Hmm, im not into android coding but in Desktop one might be able to use
              http://doc.qt.io/qt-5/qwidget.html#nativeEvent

              But in link the
              Override public boolean dispatchKeyEvent(KeyEvent event)
              seems to be something else.

              It says that
              "This code should be put into your applications' activity Java code. Your activity will need to inherit from QtActivity.
              You'll also need to add static native methods to your activity which you call in the appropriate case.
              That allows you to make the transition into the C++ part of your application which registers its functions with the JNI in JNI_OnLoad (see https://www.kdab.com/qt-android-episode-5/)."

              and that https://www.kdab.com/qt-android-episode-5/ says
              "Why do we need JNI?"
              "Because it is impossible for Qt to implement all Android features. "

              So it sounds to me you need to mix in some java for this (catch keys) - but info is some years old
              so maybe nowadays there
              is non java way ?

              1 Reply Last reply
              1
              • SeeLookS Offline
                SeeLookS Offline
                SeeLook
                wrote on last edited by
                #7

                @Devopia53
                It was really very simple....
                And makes impression....
                I still can't breathe.....

                Thank You so much.

                M 1 Reply Last reply
                2
                • SeeLookS SeeLook

                  @Devopia53
                  It was really very simple....
                  And makes impression....
                  I still can't breathe.....

                  Thank You so much.

                  M Offline
                  M Offline
                  mvuori
                  wrote on last edited by
                  #8

                  Your mileage may vary...

                  I put the qputenv() call in my camera app. It slowed the app, making viewfinder jerky & camera would not focus (tried in one environment only: Sony Xperia Z5 Compact, Android Marshmallow, Qt 5.8 beta 597) -- in other application types the problem would perhaps be not noticed...

                  1 Reply Last reply
                  0
                  • SeeLookS Offline
                    SeeLookS Offline
                    SeeLook
                    wrote on last edited by
                    #9

                    @mvuori
                    qputenv() just sets system environment variable which can be quite innocent and silent or blow up something...
                    I hope this one of mine was rather kind of quiet :-)
                    But thanks for warning. Better blow on a cold....

                    1 Reply Last reply
                    1
                    • SeeLookS Offline
                      SeeLookS Offline
                      SeeLook
                      wrote on last edited by
                      #10

                      Hi again,
                      If there still will be a need to handle Android volume after taking control on those keys, here is a way how to do it from Qt app.

                      I made a Java class that cooperates with AudioManager - an Android native object to deal with i.e. stream volume:

                      
                      package app.of.domain;
                      
                      import org.qtproject.qt5.android.QtNative;
                      import android.content.Context;
                      import android.media.AudioManager;
                      
                      
                          /** Manages a volume level of output stream  */
                      public class OutVolume {
                      
                              /** Common method to get Android audio manager object */
                        private static AudioManager am() {
                          Context c = QtNative.activity().getApplicationContext();
                          return (AudioManager)c.getSystemService(c.AUDIO_SERVICE);
                        }
                      
                              /** Displays native Android out volume control */
                        public static void show() {
                          am().adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_SAME, AudioManager.FLAG_SHOW_UI);
                        }
                      
                              /** Maximal value of volume (depends on device) */
                        public static int maxStreamVolume() {
                          return am().getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                        }
                      
                              /** Current volume */
                        public static int streamVolume() {
                          return am().getStreamVolume(AudioManager.STREAM_MUSIC);
                        }
                      
                              /** Sets volume */
                        public static void setStreamVolume(int v) {
                          am().setStreamVolume(AudioManager.STREAM_MUSIC, v, 0);
                        }
                      }
                      

                      Now, by using goods of AndroidExtras Qt module, we may invoke native volume dialog from our QT code:

                        QAndroidJniObject::callStaticMethod<void>("app/of/domain/OutVolume", "show");
                      

                      But also we may use Qt slider (QtWidgets or QtQuick) to control volume from it.

                      To set a proper range of the slider:

                      auto slider = new QSlider(this);
                      slider->setRange(0, QAndroidJniObject::callStaticMethod<jint>("app/of/domain/OutVolume", "maxStreamVolume"));
                      

                      Then to control volume we may connect valueChanged(int) signal of QSlider with our slot:

                      void OurClassWithSlider::volChangedSlot(int v) {
                        QAndroidJniObject::callStaticMethod<void>("app/of/domain/OutVolume", "setStreamVolume", "(I)V", v);
                      }
                      

                      I hope someone can benefit from that.

                      mrjjM 1 Reply Last reply
                      2
                      • SeeLookS SeeLook

                        Hi again,
                        If there still will be a need to handle Android volume after taking control on those keys, here is a way how to do it from Qt app.

                        I made a Java class that cooperates with AudioManager - an Android native object to deal with i.e. stream volume:

                        
                        package app.of.domain;
                        
                        import org.qtproject.qt5.android.QtNative;
                        import android.content.Context;
                        import android.media.AudioManager;
                        
                        
                            /** Manages a volume level of output stream  */
                        public class OutVolume {
                        
                                /** Common method to get Android audio manager object */
                          private static AudioManager am() {
                            Context c = QtNative.activity().getApplicationContext();
                            return (AudioManager)c.getSystemService(c.AUDIO_SERVICE);
                          }
                        
                                /** Displays native Android out volume control */
                          public static void show() {
                            am().adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_SAME, AudioManager.FLAG_SHOW_UI);
                          }
                        
                                /** Maximal value of volume (depends on device) */
                          public static int maxStreamVolume() {
                            return am().getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                          }
                        
                                /** Current volume */
                          public static int streamVolume() {
                            return am().getStreamVolume(AudioManager.STREAM_MUSIC);
                          }
                        
                                /** Sets volume */
                          public static void setStreamVolume(int v) {
                            am().setStreamVolume(AudioManager.STREAM_MUSIC, v, 0);
                          }
                        }
                        

                        Now, by using goods of AndroidExtras Qt module, we may invoke native volume dialog from our QT code:

                          QAndroidJniObject::callStaticMethod<void>("app/of/domain/OutVolume", "show");
                        

                        But also we may use Qt slider (QtWidgets or QtQuick) to control volume from it.

                        To set a proper range of the slider:

                        auto slider = new QSlider(this);
                        slider->setRange(0, QAndroidJniObject::callStaticMethod<jint>("app/of/domain/OutVolume", "maxStreamVolume"));
                        

                        Then to control volume we may connect valueChanged(int) signal of QSlider with our slot:

                        void OurClassWithSlider::volChangedSlot(int v) {
                          QAndroidJniObject::callStaticMethod<void>("app/of/domain/OutVolume", "setStreamVolume", "(I)V", v);
                        }
                        

                        I hope someone can benefit from that.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @SeeLook
                        Thank you for sharing :)

                        1 Reply Last reply
                        0

                        • Login

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