×
Namespaces

Variants
Actions

在后台捕捉按键

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

平台安全性
需要的签名: DevCert
能力: SwEvent

文章
翻译:
hoolee
最后由 hamishwillee 在 22 Dec 2011 编辑
  • 详细描述

这里的CKeyCapture完成了在后台进程中捕捉按键事件的功能。注意你需要使用CaptureKey()函数定义需要捕捉的按键(如EKeyDevice0键)

同样注意如果按键是焦点程序也需要处理的,那你应该手工发送过去。在示例中的RunL()已经完成。如果按键需要传递到焦点程序,那回调函数就返回EFalse,如果已经处理完毕则返回ETrue.

示例代码可以在所有平台上工作,当使用S60第三版手机时你需要拥有SwEvent能力。

如果是前台程序捕捉按键事件,可以在CCoeControl派生类中完成标准的OfferKeyEventL()方法,如果你的程序没有按标准程序框架走,那可以尝试CKeyCapturer2方法实现。

注意:这个方法无法捕捉音量键或媒体键,如耳机上的按键

如果需要上述实现,请参考TSS000432 - Utilising media keys

  • 需要的头文件
#include <w32std.h>
#include <apgwgnam.h> //CApaWindowGroupName
  • 需要的链接库
LIBRARY   ws32.lib
LIBRARY apgrfx.lib //CApaWindowGroupName
  • 需要的能力
capability    SwEvent
  • CapturingKeys.cpp
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),iHandle(-1)
{
}
 
CKeyCapturer::~CKeyCapturer()
{
if(iHandle > -1)
{
iWg.CancelCaptureKey(iHandle);
}
 
iHandle = -1;
 
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);
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();
}
  • CapturingKeys.h
class MKeyCallBack 
{
public:
virtual TBool KeyCapturedL(TWsEvent aEvent) = 0;
};
 
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;
TInt iHandle;
};

如果需要捕捉按键以便给类似于Google Maps或Nokia Maps的程序用,当需要在RunL函数中进行如下处理以便获得长按按键时多

个事件

void CKeyCapturer::RunL()
{
if (iStatus == KErrNone)
{
TWsEvent e;
iWsSession.GetEvent(e);
 
//Only if iRepeats is 0 we should forward the event
if(e.Key()->iRepeats == 0 && iObserver.KeyCapturedL(e))
{
TInt wgId = iWsSession.GetFocusWindowGroup();
iWsSession.SendEventToWindowGroup(wgId, e);
}
}
 
if (iStatus != KErrCancel)
{
Listen();
}
}
This page was last modified on 22 December 2011, at 07:43.
95 page views in the last 30 days.
×