首 页 | 新 闻 | Symbian | Android| Windows Mobile | J2ME | 下载中心 | 游戏策划招聘与求职 | 购书指南 | 视频教程
您现在的位置: 开发视界 >> Symbian >> Symbian开发 >> 正文
后台运行的程序要如何获取按键事件
作者:symbianb…    文章来源:symbianblog    更新时间:2008-8-9 16:38:16

基本思路:

首先程序运行时注册按键事件,然后将程序转入后台运行,当按键事件发生后在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最后能处理。

相关文章:
没有相关文章
 

站点地图 | 加入收藏 | 联系站长 | 广告服务 |
QQ:280529124  Tel:0592-8271361 辽ICP备05021703号