Handling key events in your application

The application framework offers key events to UI controls on the control stack by calling CCoeControl::OfferKeyEventL() for each UI control until the key event is consumed or until there are no more UI controls on the control stack. To handle an event in a UI control, you must override CCoeControl::OfferKeyEventL(). If a UI control is a compound control, then forward key events to other controls within the compound control. The control that consumes the event must be a window-owning control. An example of an implementation is as follows:

KeyResponse CMyAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
    {
    TKeyResponse response = EKeyWasNotConsumed;
    //Check first that event type is EEventKey. It could be also
    //EEventKeyUp or EEventKeyDown which just inform that some key
    // is pressed or released.
    if( aType == EEventKey )
        {
        // Take out the code of the key
        TInt code = aKeyEvent.iCode;
        TBuf< KMessageLength > buf;
        buf.AppendNum( code );
        // Show the code
        iEikonEnv->InfoMsg(buf);
        // Forward the key event to the focused control
        if( iListBox->IsFocused() )
            {
            response = iListBox->OfferKeyEventL( aKeyEvent, aType );
            }
        else if( iEditView->IsFocused() )
            {
            response =
            iEditView->OfferKeyEventL( aKeyEvent, aType );
            }
        }
        // Return the key consumption status
        return response;
    }

CMyAppView is a window-owning compound control and iListBox and iEditView are controls inside CMyAppView. Key events are offered to controls depending on the focus.

If your implementation consumes the event handed to it, you must return EKeyWasConsumed. If your implementation does not process the event, your code must return EKeyWasNotConsumed. The application framework continues to offer the event to controls registered with the control stack from top to bottom until the event is consumed or until there are no more controls, in which case the event is handed to the UI controller by calling CCoeAppUI::HandleKeyEventL() for the UI controller.

Handle key events in your UI controller by overriding CCoeAppUI::HandleKeyEventL(). An example of an implementation is as follows:

TKeyResponse CMyViewAppUi::HandleKeyEventL( const TKeyEvent& aKeyEvent,TEventCode /*aType*/)
    {
    if( iTabGroup == NULL )
        {
        return EKeyWasNotConsumed;
        }

    TInt active = iTabGroup->ActiveTabIndex();
    TInt count = iTabGroup->TabCount();

    switch( aKeyEvent.iCode )
        {
        case EKeyLeftArrow:
            if ( active > 0 )
                {
                active--;
                iTabGroup->SetActiveTabByIndex( active );
                // ActivateLocalViewL() is used to change the view. 
                // To change view from another application we would use ActivateViewL()
                // Send an empty message
                ActivateLocalViewL( TUid::Uid( iTabGroup->TabIdFromIndex( active ) ) );
                }
            break;
        case EKeyRightArrow:
            if( ( active + 1 ) < count )
                {
                active++;
                iTabGroup->SetActiveTabByIndex( active );
                // ActivateLocalViewL() is used to change the view. 
                // To change view from another application we would use ActivateViewL()
                ActivateLocalViewL( TUid::Uid( iTabGroup->TabIdFromIndex( active ) ) );
                }
            break;
        default:
            return EKeyWasNotConsumed;
            break;
        }

    return EKeyWasConsumed;
    }

CMyAppView is a UI controller that is using key events from the selection key of a device to switch between two CAknView-derived views. EKeyLeftArrow and EKeyRightArrow are codes enumerated in TKeyCode and are contained in KeyEvent.iCode.

Note: The above code snippet was taken from the MyView example application provided in the Platform Application Views package available from Nokia Developer .

See TKeyCode for more information.