## Capturing keys Background or Foreground

Here is an example for End Key (Red Key)

Library:

Capability: SWEvent

Header file: ForeGroundObserver.h

#include <w32std.h>
#include <APGWGNAM.H> //CApaWindowGroupName

/*class MFgrCallBack
{
public:
    virtual void ForegroundEventL(TUid aAppUid) = 0;
};*/

class CFgrObserver : public CActive
{

public:

static CFgrObserver* NewL(RWsSession& aWsSession/*,MFgrCallBack& aCallBack*/);
static CFgrObserver* NewLC(RWsSession& aWsSession/*,MFgrCallBack& aCallBack*/);
virtual ~CFgrObserver();

private:

CFgrObserver(RWsSession& aWsSession/*,MFgrCallBack& aCallBack*/);
void ConstructL();
void RunL();
void DoCancel();
void Listen();
private:
/*    MFgrCallBack&     iCallBack; */
RWsSession&     iWsSession;
RWindowGroup    iWg;

public:

TInt iEndKey1,iEndKey2 ;

};

Cpp file: ForeGroundObserver.cpp

#include "ForeGroundObserver.h"
#include "eikbtgpc.h"

CFgrObserver* CFgrObserver::NewL(RWsSession& aWsSession/*,MFgrCallBack& aCallBack*/)
{
CFgrObserver* self = CFgrObserver::NewLC(aWsSession/*,aCallBack*/);
CleanupStack::Pop(self);
return self;
}

CFgrObserver* CFgrObserver::NewLC(RWsSession& aWsSession/*,MFgrCallBack& aCallBack*/)
{
CFgrObserver* self = new (ELeave) CFgrObserver(aWsSession/*,aCallBack*/);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}

CFgrObserver::CFgrObserver(RWsSession& aWsSession/*,MFgrCallBack& aCallBack*/)
:CActive(EPriorityHigh),/*iCallBack(aCallBack),*/ iWsSession(aWsSession), iWg(aWsSession)
{
}

CFgrObserver::~CFgrObserver()
{
Cancel();
iWg.Close();

////////////
RWindowGroup& groupWin = CCoeEnv::Static()->RootWin();
groupWin.CancelCaptureKeyUpAndDowns( iEndKey1 );
groupWin.CancelCaptureKey( iEndKey2 );
///////////
}

void CFgrObserver::ConstructL()
{
CActiveScheduler::Add(this);

User::LeaveIfError(iWg.Construct((TUint32)&iWg, EFalse));
iWg.SetOrdinalPosition(-1);
iWg.EnableReceiptOfFocus(EFalse);

CApaWindowGroupName* wn=CApaWindowGroupName::NewLC(iWsSession);
wn->SetHidden(ETrue);
wn->SetWindowGroupName(iWg);
CleanupStack::PopAndDestroy();

User::LeaveIfError(iWg.EnableFocusChangeEvents());

Listen();
}

void CFgrObserver::RunL()
{
if (iStatus == KErrNone)
{
TWsEvent e;
iWsSession.GetEvent(e);
}

TInt wgid = iWsSession.GetFocusWindowGroup();

CApaWindowGroupName* gn;
gn = CApaWindowGroupName::NewLC(iWsSession, wgid);

///////////////
TUid appuid1= {0x20027DAE};
TUid appuid2= {0x100056CF};
/*   RWindowGroup wg  = iEikonEnv->RootWin(); */
RWindowGroup& wg = CCoeEnv::Static()->RootWin();
if(gn->AppUid() == appuid1)
{
/* We are in the foreground , so capture the keys */
if (iEndKey1 <=0)
{
iEndKey1= wg.CaptureKeyUpAndDowns( EStdKeyNo, 0, 0 );
}
if (iEndKey2 <=0)
{
iEndKey2 = wg.CaptureKey( EKeyNo, 0, 0 );
}
}
else if (gn->AppUid() == appuid2)
{
/* We are in the foreground , so capture the keys */
if (iEndKey1 <=0)
{
iEndKey1= wg.CaptureKeyUpAndDowns( EStdKeyNo, 0, 0 );
}
if (iEndKey2 <=0)
{
iEndKey2 = wg.CaptureKey( EKeyNo, 0, 0 );
}
}
else
{
/* We are in the background  , so cancel the capture keys */
if (iEndKey1 >0)
{
wg.CancelCaptureKeyUpAndDowns(iEndKey1);
iEndKey1 =0;
}
if (iEndKey2 >0)
{
wg.CancelCaptureKey(iEndKey2);
iEndKey2 = 0;
}
}
////////////////
/*   iCallBack.ForegroundEventL(gn->AppUid()); */

CleanupStack::PopAndDestroy(); // gn

if (iStatus != KErrCancel)
{

Listen();

}
}

void CFgrObserver::DoCancel()
{
iWsSession.EventReadyCancel();
}

void CFgrObserver::Listen()
{
iWsSession.EventReady(&iStatus);
SetActive();
}

Declare in AppUI header file:

CFgrObserver* iFgrObserver;
RWsSession      iWsSession;

Declare in AppUI cpp file:

In Destructor:

delete iFgrObserver;
iWsSession.Close();

In ConstructorL:

User::LeaveIfError(iWsSession.Connect());
iFgrObserver = CFgrObserver::NewL(iWsSession/*, *this*/);