//------------------------------------------------------程序隐藏到后台-----------------------------------------------------
TApaTask task(iEikonEnv->WsSession());
task.SetWgId(CEikonEnv::Static()->RootWin().Identifier());
task.SendToBackground();
//HideApplicationFromFSW(ETrue);
//ETrue的时候,在主界面的任务列表中不显示本程序,EFalse显示本程序
//------------------------------------------------------程序显示到前台-----------------------------------------------------
TApaTask task(iEikonEnv->WsSession());
task.SetWgId(CEikonEnv::Static()->RootWin().Identifier());
task.BringToForeground();
//------------------------------------------------------程序后台监听某些键值-----------------------------------------------------
//这一般都是配合热键而用的,也就是当我隐藏到后台后,返回到主页面上,按下某个特定的键,就会调用此程序。
//------------------------------------------------------头文件-----------------------------------------------------
#include <w32std.h>
#include <apgwgnam.h> //CApaWindowGroupName
class MKeyCallBack
{
public:
virtual TBool KeyCapturedL(TWsEvent aEvent) = 0;
};
class CHandleItem
{
public:
CHandleItem();
~CHandleItem();
TInt iHandle;
};
class CKeyCapturer : public CActive
{
public:
static CKeyCapturer* NewL(MKeyCallBack& aObserver);
static CKeyCapturer* NewLC(MKeyCallBack& aObserver);
virtual ~CKeyCapturer();
private:
CKeyCapturer(MKeyCallBack& aObserver);
void ConstructL();
void RunL();
void DoCancel();
void Listen();
private:
MKeyCallBack& iObserver;
RWsSession iWsSession;
RWindowGroup iWg;
RPointerArray<CHandleItem> iHandleArray;
};
//------------------------------------------------------定义文件-----------------------------------------------------
#include "CapturingKeys.h"
CHandleItem::CHandleItem()
{
}
CHandleItem::~CHandleItem()
{
}
CKeyCapturer* CKeyCapturer::NewL(MKeyCallBack& aObserver)
{
CKeyCapturer* self = CKeyCapturer::NewLC(aObserver);
CleanupStack::Pop(self);
return self;
}
CKeyCapturer* CKeyCapturer::NewLC(MKeyCallBack& aObserver)
{
CKeyCapturer* self = new (ELeave) CKeyCapturer(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CKeyCapturer::CKeyCapturer(MKeyCallBack& aObserver) :
CActive(EPriorityStandard), iObserver(aObserver)
{
}
CKeyCapturer::~CKeyCapturer()
{
/*
if (iHandle > -1)
{
iWg.CancelCaptureKey(iHandle);
}
iHandle = -1;
*/
//注销
TInt nCount = iHandleArray.Count();
for (TInt i = 0; i < nCount; i++)
{
if (iHandleArray[i]->iHandle > -1)
{
iWg.CancelCaptureKey(iHandleArray[i]->iHandle);
}
iHandleArray[i]->iHandle = -1;
}
//
iHandleArray.ResetAndDestroy();
Cancel();
iWg.Close();
iWsSession.Close();
}
void CKeyCapturer::ConstructL()
{
CActiveScheduler::Add(this);
User::LeaveIfError(iWsSession.Connect());
iWg = RWindowGroup(iWsSession);
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();
//iHandle = iWg.CaptureKey(EKeyDevice0, 0,0);
TInt nStart = 14;
TInt nEnd = 17;
for (TInt i = nStart; i <= nEnd; i++)
{
CHandleItem * pItem = new (ELeave) CHandleItem;
pItem->iHandle = iWg.CaptureKey(i, 0, 0);
iHandleArray.Append(pItem);
}
nStart = 48;
nEnd = 57;
for (TInt i = nStart; i <= nEnd; i++)
{
CHandleItem * pItem = new (ELeave) CHandleItem;
pItem->iHandle = iWg.CaptureKey(i, 0, 0);
iHandleArray.Append(pItem);
}
nStart = 164;
nEnd = 167;
for (TInt i = nStart; i <= nEnd; i++)
{
CHandleItem * pItem = new (ELeave) CHandleItem;
pItem->iHandle = iWg.CaptureKey(i, 0, 0);
iHandleArray.Append(pItem);
}
Listen();
}
void CKeyCapturer::RunL()
{
if (iStatus == KErrNone)
{
TWsEvent e;
iWsSession.GetEvent(e);
if (iObserver.KeyCapturedL(e))
{
TInt wgId = iWsSession.GetFocusWindowGroup();
iWsSession.SendEventToWindowGroup(wgId, e);
}
}
if (iStatus != KErrCancel)
{
Listen();
}
}
void CKeyCapturer::DoCancel()
{
iWsSession.EventReadyCancel();
}
void CKeyCapturer::Listen()
{
iWsSession.EventReady(&iStatus);
SetActive();
}