Symbian, Blog, Innovation

如何使后台运行的程序获取按键事件

09月 22nd, 2007

基本思路:

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

因为这个问题,研究了近3个小时,sigh。。。

Popularity: 8% [?]

Entry Filed under: Symbian代码研究, Symbian开发, Symbian开发技巧

1 Comment Add your own

  • 1.  |  12月 27th, 2007 at 2:58 pm

    Google…

    Google is the best search engine Google…

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


标签

Calendar

09月 2007
    Oct »
 12
3456789
10111213141516
17181920212223
24252627282930

Most Recent Posts