后台运行程序捕获按键

1 在C***AppUi::ConstructL()里面调用SetCaptureKey()

 

  void CTelephoneAppUi::ConstructL() { // [[[ begin generated region: do not modify [Generated Contents] BaseConstructL(EAknEnableSkin); InitializeContainersL(); // ]]] end generated region [Generated Contents] // TApaTask self(iEikonEnv->WsSession()); self.SetWgId(iEikonEnv->RootWin().Identifier()); self.SendToBackground(); SetCaptureKey(); } void CTelephoneAppUi::SetCaptureKey() { // If there is another handle, we have to cancel it first. // CancelCaptureKey(); // This will capture scan code of the keypress. // iHandleCaptureKey = //RWindowGroup //TStdScanCode CCoeEnv::Static()->RootWin().CaptureKeyUpAndDowns(EStdKeyYes, 0, 0, 0); //// 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(EStdKeyYes, 0, 0, 0); }

 

2 在TKeyResponse C***AppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,
  TEventCode aType) 里面处理按键

 

 TKeyResponse CTelephoneAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType) { // The inherited HandleKeyEventL is private and cannot be called // [[[ begin generated region: do not modify [Generated Contents] // ]]] end generated region [Generated Contents] if (aType == EEventKeyDown) { switch (aKeyEvent.iScanCode) { case EStdKeyYes: { .......; break; } } } return EKeyWasNotConsumed; }

 

 这样既可。

NOTE:

1 在Container里面处理按键事件时,注意不要捕获的按键,记得return EKeyWasNotConsumed;
以便这里处理。

2 要捕获系统按键需要添加SwEvent能力。

思路:

基本思路:

首先程序运行时注册按键事件,然后将程序转入后台运行,当按键事件发生后在AppUi的HandleKeyEventL中处理。

 

参考自网络资源。捕获接听键成功。

 

//记得把按键信息传回去

// RWsSession iWsSession;

// User::LeaveIfError(iWsSession.Connect());

// TInt wgId = iWsSession.GetFocusWindowGroup(); //获取当前界面ID

// CApaWindowGroupName* gn = CApaWindowGroupName::NewLC(iWsSession, wgId);

// TUid uid = gn->AppUid();

// TWsEvent event;

// event.SetType(EEventKey);

// event.SetTimeNow();

// event.Key()->iCode = aKeyEvent.iCode;

// event.Key()->iModifiers = aKeyEvent.iModifiers;

// event.Key()->iRepeats = aKeyEvent.iRepeats;

// event.Key()->iScanCode = aKeyEvent.iScanCode;

// iWsSession.SendEventToWindowGroup(wgId, event);

// CleanupStack::PopAndDestroy(); //gn

// iWsSession.Close();

你可能感兴趣的:(c,网络)