基本思路:
首先程序运行时注册按键事件,然后将程序转入后台运行,当按键事件发生后在AppUi的HandleKeyEventL中处理。
相关代码如下:
void CClockSSAppUi::SetCaptureKey() { // If there is another handle, we have to cancel it first. CancelCaptureKey(); // This will capture scan code of the keypress. iHandleCaptureKey = CCoeEnv::Static()-> RootWin().CaptureKeyUpAndDowns( KOkKeyScanCode, EModifierShift, EModifierShift PRIORITYCAPTUREKEY); //// WARNING: We need to capture the normal code of keypress otherwise // the key event will be sent to another application. iHandleCaptureKey2 = CCoeEnv::Static()-> RootWin().CaptureKey( KOkKeyCode, EModifierShift, EModifierShift PRIORITYCAPTUREKEY); }
TKeyResponse CClockSSAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType) { if ((KOkKeyScanCode == (TUint) aKeyEvent.iScanCode) && (EEventKeyDown == aType) && ((aKeyEvent.iModifiers & EModifierShift) == EModifierShift) ) { CAknGlobalNote* globalNote = CAknGlobalNote::NewLC(); globalNote->ShowNoteL(EAknGlobalInformationNote, _L(”Captured KEY!”)); CleanupStack::PopAndDestroy(); } }
需要注意的是:如果你的是多view程序,并且调用了AppUi::AddToStackL接收按键事件,那么在这些控件OfferKeyEventL的处理中对于不相关的键事件一定要返回EKeyWasNotConsumed,好让AppUi最后能处理。 |