//------------------------------------------头文件------------------------------------------
#ifndef __CAPTURINGKEYS__
#define __CAPTURINGKEYS__
#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;
};
#endif
//------------------------------------------定义文件------------------------------------------
#include "manager/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/*EPriorityHigh*/), iObserver(aObserver)
{
}
CKeyCapturer::~CKeyCapturer()
{
//注销
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();
TInt nStart = 0;
TInt nEnd = 0;
//0~9
nStart = 48;
nEnd = 57;
for (TInt i = nStart; i <= nEnd; i++)
{
CHandleItem * pItem = new (ELeave) CHandleItem;
pItem->iHandle = iWg.CaptureKey(i, 0, 0);
//iWg.CaptureLongKey(i, i, 0, 0, 1000000000, ELongCaptureNormal); // to start capturing
iHandleArray.Append(pItem);
}
//注册* #
CHandleItem * pItem133 = new (ELeave) CHandleItem;
pItem133->iHandle = iWg.CaptureKey(42, 0, 0); //可以捕获*
//pItem133->iHandle = iWg.CaptureKey('*', 0, 0);
iHandleArray.Append(pItem133);
CHandleItem * pItem127 = new (ELeave) CHandleItem;
//pItem127->iHandle = iWg.CaptureKey(127, 0, 0);
pItem127->iHandle = iWg.CaptureKey('#', 0, 0);
iHandleArray.Append(pItem127);
//*、#键比较郁闷,
//在手机上,*必须注册42
//在模拟器上,*必须注册133
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();
}
//------------------------------------------调用规则------------------------------------------
1.在.h文件中定义CKeyCapturer变量。并继承MKeyCallBack类。在类中实例化KeyCapturedL函数。
2.在.cpp文件的ConstructL中
//创建监听类
iKeyCapturer = CKeyCapturer::NewL(*this);
3.在.cpp文件中定义
TBool CSmartDialAppUi::KeyCapturedL(TWsEvent aEvent)
{
TInt nKey = aEvent.Key()->iScanCode;
//查看程序是否可以用
if (iControl->GetStateApp())
{
if ((42 == nKey) || (127 == nKey) || (48 <= nKey && 57 >= nKey))
{
//这就是监听到的键值
}
}
return ETrue;
}
注册监听键值 0~9、*、#键
最新推荐文章于 2020-08-13 16:14:03 发布