Thursday, September 4, 2008

Sending S60 Application to Background & bringing back to Foreground

Minimizing and Maxmizing S60 Application
Key Events
When ever user presses a key, Key down and Key event will be generated by system and when releasing the key, key up event will be generated by system. This is not only in Symbian but all Operating system is designed like this.

How to capture these three key events in S60?
1. RWindowGroup:: CaptureKeyUpAndDowns()
2. RWindowGroup:: CaptureKey()


These two functions are used to capture three event for a single key press and release events. (i.e) EEventKey, EEventKeyUp, EEventKeyDown.

---->Capturing keys are like redirecting the normal behavior of system. Once we done with the work for a particular key, we need to pass the key to window server so that normal behavior will not get spoiled. Most of the example will not send the captured key (if they are not capturing the key then they will send the key to WS) to window server so, you cannot expect correct behavior.

---->This below code is for sending a application to background and bringing back to foreground. When this application is in foreground press END key to send it to background and if you press HASH (#) the same application will come to foreground.


MMPFile
CAPABILITY SwEvent

Header file
#include "w32std.h"
#include "e32keys.h"
#include "apgtask.h"

Add these in AppUi header file
TInt iEndKeyUpDown , iEndKeyEvent, iHashKeyUpDown, iHashKeyEvent;

Add these lines in AppUi::ConstructL()
RWindowGroup& groupWin = CEikonEnv::Static()->RootWin();
iEndKeyUpDown = groupWin.CaptureKeyUpAndDowns( EStdKeyNo, 0, 0 );
iEndKeyEvent = groupWin.CaptureKey( EStdKeyNo, 0, 0 );
iHashKeyUpDown = groupWin.CaptureKeyUpAndDowns ( ‘#’, 0, 0 );
iHashKeyEvent = groupWin.CaptureKey(‘#’, 0 , 0);

Override function in AppUi
void HandleWsEventL (const TWsEvent &aEvent, CCoeControl *aDestination);
{
TApaTaskList taskList2(iEikonEnv->WsSession());
TApaTask task2 = taskList2.FindApp(TUid::Uid(0xE9F96B59));
TInt key = aEvent.Key()->iScanCode;

if (EStdKeyHash == key && aEvent.Type() == EEventKey)
task2.BringToForeground();

if(EStdKeyNo == key && aEvent.Type() == EEventKey)
task2.SendToBackground();

fSession.Close();
CAknAppUi::HandleWsEventL(aEvent,aDestination);
}

Add in Destructor of AppUi

RWindowGroup& groupWin = CEikonEnv::Static()->RootWin();
groupWin.CancelCaptureKeyUpAndDowns(iEndKeyUpDown );
groupWin.CancelCaptureKey( iEndKeyEvent );
groupWin.CancelCaptureKeyUpAndDowns( iHashKeyUpDown);
groupWin.CancelCaptureKey( iHashKeyEvent );



RWindowGroup
Every application has its own Window group object, this is used to register an application behavior into window server, like when ever window server receives a key event where should it pass.

TApaTaskList : This class is used to get the list of running tasks. This will ask window server to get list of task for that TApaTaskList class need a window server session, so we are giving our application’s window server session which is maintained by framework.

TApaTask: This class is for holding information about a particular task so we can bring the application to foreground or background or kill etc….
:-).....

No comments: