Bring application to foreground with a keypress
From Forum Nokia Wiki
Inorder to capture the keys while you application under background you've to override CCoeAppUi::HandleWsEventL to your AppUi. With the use of RWindowGroup::CaptureKeyUpAndDowns() you can capture the keys from an application. Below is some sample code which will show you basically how to do it, you may found some additional bit of code which you may use.
The mmp file
CAPABILITY SwEvent
The header file
/**
* To store the handle to the captured key.
*/
TInt iHashKeyHandle;
The cpp file
// -----------------------------------------------------------------------------
// CMyAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CMyAppUi:: ConstructL ()
{
BaseConstructL() ;
// ...
// set application as system application so that it will
// not be closed by system events
CEikonEnv:: Static () - > SetSystem( ETrue ) ;
// capture hash '#' key permanently
iHashKeyHandle = CEikonEnv:: Static () - > RootWin() .CaptureKeyUpAndDowns ( EStdKeyHash, 0, 0) ;
// set application priority to foreground priority even if it goes to background
CEikonEnv:: Static () - > WsSession() .ComputeMode ( RWsSession:: EPriorityControlDisabled ) ;
// Make the application a high priority application
CEikonEnv:: Static () - > RootWin() .EnableReceiptOfFocus ( ETrue ) ;
CEikonEnv:: Static () - > RootWin() .SetOrdinalPosition ( 0, ECoeWinPriorityAlwaysAtFront) ;
}
// -----------------------------------------------------------------------------
// CMyAppUi::~CMyAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CMyAppUi:: ~CMyAppUi()
{
// Release the hanlde to the captured key.
CEikonEnv:: Static () - > RootWin() .CancelCaptureKeyUpAndDowns ( iHashKeyHandle) ;
// ...
}
// -----------------------------------------------------------------------------
// CMyAppUi::HandleForegroundEventL(TBool aForeground)
// Handles changes in keyboard focus when the application is brought to the
// foreground, or put into the background.
// -----------------------------------------------------------------------------
//
void CMyAppUi:: HandleForegroundEventL ( TBool aForeground)
{
// Make the application a high priority application
if ( aForeground)
{
CEikonEnv:: Static () - > RootWin() .SetOrdinalPosition ( 0, ECoeWinPriorityAlwaysAtFront) ;
}
}
// -----------------------------------------------------------------------------
// CMyAppUi::HandleWsEventL (const TWsEvent &aEvent, CCoeControl *aDestination)
// Handles events sent to the application by the window server.
// -----------------------------------------------------------------------------
//
void CMyAppUi:: HandleWsEventL ( const TWsEvent & aEvent, CCoeControl * aDestination)
{
if ( aEvent.Type () == EEventKey ||
aEvent.Type () == EEventKeyUp ||
aEvent.Type () == EEventKeyDown ||
aEvent.Type () == EEventKeyRepeat)
{
// This is for switching back to Application when Hash key is pressed
if ( EStdKeyHash == aEvent.Key () - > iScanCode && EEventKey == aEvent.Type ())
{
TApaTask task( iEikonEnv- > WsSession()) ;
task.SetWgId ( CEikonEnv:: Static () - > RootWin() .Identifier ()) ;
task.BringToForeground () ;
return ;
}
}
CAknAppUi:: HandleWsEventL ( aEvent, aDestination) ;
}

本文介绍了一种方法,使应用程序能够在后台捕获按键事件,并通过按下特定按键(如'#'键)将应用程序从后台切换到前台。这种方法涉及设置应用程序为系统级应用、永久捕获指定按键、调整应用优先级等步骤。
3131

被折叠的 条评论
为什么被折叠?



