注册监听键值 0~9、*、#键



//------------------------------------------头文件------------------------------------------

#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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值