English Български

Getting Audio Volume Key Event on Symbian Devices

In QML on Symbian devices, audio volume key events are not delivered via Keys.onVolumeDownPressed, Keys.onVolumeUpPressed, or Keys.onPressed. An appropriate solution is wrapping around S60 Remote Control API [wiki.forum.nokia.com] by inheriting MRemConCoreApiTargetObserver [library.forum.nokia.com]. This is an example code how to do it when building QML/C++ app. Making QML plugin should not be too far from this.

Create a new QML element to deliver the audio volume key event. The S60 Remote Control API actually can get other media key events too. Refer the link above for more detail.

MediakeyCaptureItem.h

  1. #ifndef MediakeyCaptureItem_H
  2. #define MediakeyCaptureItem_H
  3.  
  4. #include <QDeclarativeItem>
  5.  
  6. #ifdef Q_WS_S60
  7. #include <remconcoreapitargetobserver.h>    // link against RemConCoreApi.lib
  8. #include <remconcoreapitarget.h>            // and
  9. #include <remconinterfaceselector.h>        // RemConInterfaceBase.lib
  10.  
  11. class MediakeyCaptureItemPrivate;
  12. class MediakeyCaptureItem : public QDeclarativeItem
  13. {
  14.     Q_OBJECT
  15. public:
  16.     MediakeyCaptureItem(QDeclarativeItem *parent = 0);
  17.     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
  18.  
  19. signals:
  20.     void volumeDownPressed();
  21.     void volumeUpPressed();
  22.  
  23. private:
  24.     MediakeyCaptureItemPrivate *d_ptr;
  25. private:    // Friend class definitions
  26.     friend class MediakeyCaptureItemPrivate;
  27. };
  28.  
  29. #endif // Q_WS_S60
  30. #endif // MediakeyCaptureItem_H

MediakeyCaptureItem.cpp

  1. #include "MediaKeyCaptureItem.h"
  2. #ifdef Q_WS_S60
  3.  
  4. // A private class to access Symbian RemCon API
  5. class MediakeyCaptureItemPrivate : public QObject, public MRemConCoreApiTargetObserver
  6. {
  7. public:
  8.     MediakeyCaptureItemPrivate(MediakeyCaptureItem *parent);
  9.     ~MediakeyCaptureItemPrivate();
  10.     virtual void MrccatoCommand(TRemConCoreApiOperationId aOperationId,
  11.                                             TRemConCoreApiButtonAction aButtonAct);
  12. private:
  13.     CRemConInterfaceSelector* iInterfaceSelector;
  14.     CRemConCoreApiTarget*     iCoreTarget;
  15.     MediakeyCaptureItem *d_ptr;
  16. };
  17.  
  18. // Consructor
  19. MediakeyCaptureItem::MediakeyCaptureItem(QDeclarativeItem *parent): QDeclarativeItem(parent)
  20. {
  21.     d_ptr = new MediakeyCaptureItemPrivate(this);
  22. }
  23.  
  24. // The paint method
  25. void MediakeyCaptureItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  26. {
  27.     // This item has no visual
  28. }
  29.  
  30. // Constructor
  31. MediakeyCaptureItemPrivate::MediakeyCaptureItemPrivate(MediakeyCaptureItem *parent): d_ptr(parent)
  32. {
  33.     QT_TRAP_THROWING(iInterfaceSelector = CRemConInterfaceSelector::NewL());
  34.     QT_TRAP_THROWING(iCoreTarget = CRemConCoreApiTarget::NewL(*iInterfaceSelector, *this));
  35.     iInterfaceSelector->OpenTargetL();
  36. }
  37.  
  38. // Destructor
  39. MediakeyCaptureItemPrivate::~MediakeyCaptureItemPrivate(){
  40.     delete iInterfaceSelector;
  41.     delete  iCoreTarget;
  42. }
  43.  
  44. // Callback when media keys are pressed
  45. void MediakeyCaptureItemPrivate::MrccatoCommand(TRemConCoreApiOperationId aOperationId,
  46.                                         TRemConCoreApiButtonAction aButtonAct)
  47. {
  48.     //TRequestStatus status;
  49.     switch( aOperationId )
  50.     {
  51.     case ERemConCoreApiVolumeUp:
  52.         emit d_ptr->volumeUpPressed();
  53.         break;
  54.     case ERemConCoreApiVolumeDown:
  55.         emit d_ptr->volumeDownPressed();
  56.         break;
  57.     default:
  58.         break;
  59.     }
  60. }
  61. #endif // Q_WS_S60

In the C++ code, register it into the declarative engine.

  1. qmlRegisterType<MediakeyCaptureItem>("Mediakey", 1, 0, "MediakeyCapture");

Then, you need to add some lines in your .pro file to build.

  1. symbian{
  2.     INCLUDEPATH += MW_LAYER_SYSTEMINCLUDE // Not sure if this is needed...
  3.     LIBS += -L\epoc32\release\armv5\lib -lremconcoreapi
  4.     LIBS += -L\epoc32\release\armv5\lib -lremconinterfacebase
  5. }

In QML, just listen to the signals like this

  1. import Qt 4.7
  2. import Mediakey 1.0
  3. Item{
  4.  
  5.     ...
  6.  
  7.     MediakeyCapture{
  8.         onVolumeDownPressed: console.log('VOLUME DOWN PRESSED ')
  9.         onVolumeUpPressed: console.log('VOLUME UP PRESSED ')
  10.     }
  11. }

Troubleshooting

In Qt SDK 1.1 (Windows, Beta and RC) this available only for Symbian^3. But simply copy all

  1. C:\QtSDK\Symbian\SDKs\Symbian3Qt472\epoc32\release\armv5\lib\rem*
to
  1. C:\QtSDK\Symbian\SDKs\Symbian1Qt472\epoc32\release\armv5\lib
will solved compilation problem.

Categories: