CKeyCapturer2演示了如何在那些没有实现标准程序框架的程序中捕获key事件。在实现了标准程序框架的程序中你可以通过调用OfferKeyEventL来捕捉.
原文: http://wiki.forum.nokia.com/index.php/Capturing_all_keys_in_Non-GUI_applications
CKeyCapturer2
*
CKeyCapturer2::NewL(MKeyCallBack
&
aObserver)
...
{
CKeyCapturer2* self = CKeyCapturer2::NewLC(aObserver);
CleanupStack::Pop(self);
return self;
}
CKeyCapturer2
*
CKeyCapturer2::NewLC(MKeyCallBack
&
aObserver)
...
{
CKeyCapturer2* self = new (ELeave) CKeyCapturer2(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CKeyCapturer2::CKeyCapturer2(MKeyCallBack
&
aObserver)
:CActive(EPriorityHigh),iObserver(aObserver)
...
{
}
CKeyCapturer2::
~
CKeyCapturer2()
...
{
Cancel();
iWg.Close();
iWsSession.Close();
}
void
CKeyCapturer2::ConstructL()
...
{
User::LeaveIfError(iWsSession.Connect());
CActiveScheduler::Add(this);
iWg=RWindowGroup(iWsSession);
User::LeaveIfError(iWg.Construct((TUint32)&iWg, EFalse));
iWg.SetOrdinalPosition(1, ECoeWinPriorityAlwaysAtFront+2);
iWg.EnableReceiptOfFocus(ETrue);
CApaWindowGroupName* wn=CApaWindowGroupName::NewLC(iWsSession);
wn->SetHidden(EFalse);
wn->SetWindowGroupName(iWg);
CleanupStack::PopAndDestroy();
Listen();
}
void
CKeyCapturer2::RunL()
...
{
if (iStatus == KErrNone) 
...{
TWsEvent e;
iWsSession.GetEvent(e);
TInt type = e.Type();
switch (type)
...{
case EEventKey:
if(iObserver.KeyCapturedL(e))
...{
TInt wgId = iWsSession.GetFocusWindowGroup();
iWsSession.SendEventToWindowGroup(wgId, e);
}
break;
case EEventKeyUp:
case EEventKeyDown:
break;
};
}
if (iStatus != KErrCancel) 
...{
Listen();
}
}
void
CKeyCapturer2::DoCancel()
...
{
iWsSession.EventReadyCancel();
}
void
CKeyCapturer2::Listen()
...
{
iWsSession.EventReady(&iStatus);
SetActive();
}
[edit] CapturingKeys2.h
class
MKeyCallBack 
...
{
public:
virtual TBool KeyCapturedL(TWsEvent aEvent) = 0;
}
;
class
CKeyCapturer2 :
public
CActive 
...
{
public:
static CKeyCapturer2* NewL(MKeyCallBack& aObserver);
static CKeyCapturer2* NewLC(MKeyCallBack& aObserver);
virtual ~CKeyCapturer2();
private:
CKeyCapturer2(MKeyCallBack& aObserver);
void ConstructL();
void RunL();
void DoCancel();
void Listen();
private:
MKeyCallBack& iObserver;
RWsSession iWsSession;
RWindowGroup iWg;
}
;
CKeyCapturer2按键捕获
本文介绍了一个名为CKeyCapturer2的类,该类用于在未使用标准程序框架的应用程序中捕获按键事件。文章详细展示了CKeyCapturer2的实现方式及其关键方法。
1226

被折叠的 条评论
为什么被折叠?



