Archived:Utilising media keys in Symbian C++
Article Metadata
Tested with
Compatibility
S60 3rd Edition, FP1
S60 3rd Edition, FP2
Article
Description
Key presses of media keys cannot be detected in the same way as other key events. Media keys, such as Play/Pause, Stop, Volume Up/Down, Rewind, and Forward keys featured on some S60 3rd Edition devices do not generate normal key events that could be handled within the application framework, for example, in HandleKeyEventL() or OfferKeyEventL().
Solution
Events from media keys can be handled with the Remote Control API. Below is a code snippet that demonstrates this.
Remote Control API requires ReadUserData capability.
#include <remconcoreapitargetobserver.h> // link against RemConCoreApi.lib
#include <remconcoreapitarget.h> // and
#include <remconinterfaceselector.h> // RemConInterfaceBase.lib
class CMediaKeysTestUi : public CAknAppUi,
public MRemConCoreApiTargetObserver
{
...
// From MRemConCoreApiTargetObserver
void MrccatoCommand(TRemConCoreApiOperationId aOperationId,
TRemConCoreApiButtonAction aButtonAct);
// following functions from MRemConCoreApiTargetObserver are not needed
// in this case -> use empty implementations for these:
// MrccatoPlay
// MrccatoTuneFunction
// MrccatoSelectDiskFunction
// MrccatoSelectAvInputFunction
// MrccatoSelectAudioInputFunction
private:
CRemConInterfaceSelector* iInterfaceSelector;
CRemConCoreApiTarget* iCoreTarget;
};
void CMediaKeysTestUi::ConstructL()
{
...
iInterfaceSelector = CRemConInterfaceSelector::NewL();
iCoreTarget = CRemConCoreApiTarget::NewL(*iInterfaceSelector, *this);
iInterfaceSelector->OpenTargetL();
}
// ----------------------------------------------------------------------------
// MrccatoCommand()
// Receives events (press/click/release) from the following buttons:
// ’Play/Pause’, ’Volume Up’, ’Volume Down’, ’Stop’, ’Rewind’, ’Forward’
// ----------------------------------------------------------------------------
void CMediaKeysTestUi::MrccatoCommand(TRemConCoreApiOperationId aOperationId,
TRemConCoreApiButtonAction aButtonAct)
{
TRequestStatus status;
switch( aOperationId )
{
case ERemConCoreApiPausePlayFunction:
{
switch (aButtonAct)
{
case ERemConCoreApiButtonPress:
// Play/Pause button pressed
break;
case ERemConCoreApiButtonRelease:
// Play/Pause button released
break;
case ERemConCoreApiButtonClick:
// Play/Pause button clicked
break;
default:
// Play/Pause unknown action
break;
}
//Send the response back to Remcon server
iCoreTarget->PausePlayFunctionResponse(status, KErrNone);
User::WaitForRequest(status);
break;
}
case ERemConCoreApiStop:
{
switch (aButtonAct)
{
// see above (case ERemConCoreApiPausePlayFunction)
// for possible actions
}
iCoreTarget->StopResponse(status, KErrNone);
User::WaitForRequest(status);
break;
}
case ERemConCoreApiRewind:
{
switch (aButtonAct)
{
// see above for possible actions
}
iCoreTarget->RewindResponse(status, KErrNone);
User::WaitForRequest(status);
break;
}
case ERemConCoreApiForward:
{
switch (aButtonAct)
{
// see above for possible actions
}
iCoreTarget->ForwardResponse(status, KErrNone);
User::WaitForRequest(status);
break;
}
case ERemConCoreApiVolumeUp:
{
switch (aButtonAct)
{
// see above for possible actions
}
iCoreTarget->VolumeUpResponse(status, KErrNone);
User::WaitForRequest(status);
break;
}
case ERemConCoreApiVolumeDown:
{
switch (aButtonAct)
{
// see above for possible actions
}
iCoreTarget->VolumeDownResponse(status, KErrNone);
User::WaitForRequest(status);
break;
}
case ERemConCoreApiFastForward:
{
switch (aButtonAct)
{
// see above for possible actions
}
iCoreTarget->FastForwardResponse(status, KErrNone);
User::WaitForRequest(status);
break;
}
case ERemConCoreApiBackward:
{
switch (aButtonAct)
{
// see above for possible actions
}
iCoreTarget->BackwardResponse(status, KErrNone);
User::WaitForRequest(status);
break;
}
default:
break;
}
}
Notes:
On most devices only volume keys can be used in 3rd party applications using this API.
Volume keys on accessories (for example, Nokia headsets) use different event types compared to keys on the device. Clients should handle ERemConCoreApiButtonClick, ERemConCoreApiButtonPress, and ERemConCoreApiButtonRelease events.
When pressed and held, media keys do not automatically repeat the commands. If repeat functionality is required, it has to be implemented with (CPeriodic) timers.


I've found a difference in behavior between S60 v3.0 and 3.1 devices when the application is in the background. Simply, on 3.0 devices (e.g. N73 and E61) the media keys are reported to the application anyway, while on 3.1 devices (N95 and 6120) they are not.
I was using the volume keys to bring the application to the foreground from the phone idle screen - where the volume keys aren't used anyway - on 3.0 devices. I'd be interested to know whether there's any way to get those events in the background on 3.1+ devices...
Also, I didn't add yet the User::WaitForRequest calls listed in the example, but everything seems to work OK (in applications other than my own as well), what is the purpose of these calls?
10x
Uriah
Thank your for your feedback. On most devices only volume keys can be used in 3rd party applications using this API.
Forum Nokia KB 11:14, 16 October 2007 (UTC)
Contents
Volume keys when app is in background on 3.1 devices
Contrary to what the previous poster found, I have found that applications do receive volume key events when they are in the background on the Nokia N95-1. This is with firmware 30.0.015.
Differences between commands generated for volume control on phone and volume control on headphone remote
I have found that different commands are generated when the volume control on the side of the phone is used from when the volume control on the headphone remote is used. This is on an N95-1 with 30.0.015 firmware. Full details and a link to some sample code can be found on my blog.
Thank you for your feedback. The article has been updated.
Forum Nokia KB 09:15, 5 December 2008 (EET)
CMediaKeysTestUi destructor
Article is not showing how to destroy created objects of: CRemConInterfaceSelector and CRemConCoreApiTarget. In destructor of CMediaKeysTestUi class, only CRemConInterfaceSelector object should be destroyed:
--mstrug 10:32, 8 May 2011 (UTC)
Hamishwillee - CMediaKeysTestUi destructor - Are you confident of this?
Hi Michal
Thanks. If you're confident of this then feel free to update the body of the article.
On a related note, this is archived as not relevant to current platforms - which may have been done in error. Do you know if this works on recent versions of Symbian, and if so, which ones have you tested it on? (I could then add ReviewForRemovalFromArchive to and remove this article from archive)
Regards
Hamishhamishwillee 04:53, 28 January 2013 (EET)