Symbian3/SDK/Source/GUID-1614B24F-5DB2-43AA-9A18-723BD61B8B4C.dita
author Dominic Pinkman <Dominic.Pinkman@Nokia.com>
Thu, 21 Jan 2010 18:18:20 +0000
changeset 0 89d6a7a84779
permissions -rw-r--r--
Initial contribution of Documentation_content according to Feature bug 1266 bug 1268 bug 1269 bug 1270 bug 1372 bug 1374 bug 1375 bug 1379 bug 1380 bug 1381 bug 1382 bug 1383 bug 1385

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE concept
  PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-1614B24F-5DB2-43AA-9A18-723BD61B8B4C" xml:lang="en"><title>Handling
key events in your application</title><prolog><metadata><keywords/></metadata></prolog><conbody>
<p>The application framework offers key events to UI controls on the control
stack by calling <xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Control_Environment/CCoeControlClass.html#%3a%3aCCoeControl%3a%3aOfferKeyEventL%28const%20TKeyEvent%20%26amp%3b%2cTEventCode%29" format="application/java-archive"><parmname>CCoeControl::OfferKeyEventL()</parmname></xref> 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 <xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Control_Environment/CCoeControlClass.html#%3a%3aCCoeControl%3a%3aOfferKeyEventL%28const%20TKeyEvent%20%26amp%3b%2cTEventCode%29" format="application/java-archive"><parmname>CCoeControl::OfferKeyEventL()</parmname></xref>. 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 <xref href="GUID-352850A9-227F-45DB-8DCD-C6268954B4ED.dita">window-owning control</xref>.
An example of an implementation is as follows:</p>
<codeblock id="GUID-D93EBAD0-6C04-4E97-ABE2-9999EAA6F442" xml:space="preserve">KeyResponse CMyAppView::OfferKeyEventL(const TKeyEvent&amp; 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&lt; KMessageLength &gt; buf;
        buf.AppendNum( code );
        // Show the code
        iEikonEnv-&gt;InfoMsg(buf);
        // Forward the key event to the focused control
        if( iListBox-&gt;IsFocused() )
            {
            response = iListBox-&gt;OfferKeyEventL( aKeyEvent, aType );
            }
        else if( iEditView-&gt;IsFocused() )
            {
            response =
            iEditView-&gt;OfferKeyEventL( aKeyEvent, aType );
            }
        }
        // Return the key consumption status
        return response;
    }
</codeblock>
<p><parmname>CMyAppView</parmname> is a window-owning compound control
and <parmname>iListBox</parmname> and <parmname>iEditView</parmname> are controls
inside <parmname>CMyAppView</parmname>. Key events are offered to controls
depending on the focus.</p>
<p>If your implementation consumes the event handed to it, you must return <parmname>EKeyWasConsumed</parmname>.
If your implementation does not process the event, your code must return <parmname>EKeyWasNotConsumed</parmname>.
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 <xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Control_Environment/CCoeAppUiClass.html#%3a%3aCCoeAppUi%3a%3aHandleKeyEventL%28const%20TKeyEvent%20%26amp%3b%2cTEventCode%29" format="application/java-archive"><parmname>CCoeAppUI::HandleKeyEventL()</parmname></xref> for the UI
controller. </p>
<p>Handle key events in your UI controller by overriding <xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Control_Environment/CCoeAppUiClass.html#%3a%3aCCoeAppUi%3a%3aHandleKeyEventL%28const%20TKeyEvent%20%26amp%3b%2cTEventCode%29" format="application/java-archive"><parmname>CCoeAppUI::HandleKeyEventL()</parmname></xref>. An example
of an implementation is as follows:</p>
<codeblock id="GUID-3A2D3630-C28F-4165-8589-73AA4627D008" xml:space="preserve">TKeyResponse CMyViewAppUi::HandleKeyEventL( const TKeyEvent&amp; aKeyEvent,TEventCode /*aType*/)
    {
    if( iTabGroup == NULL )
        {
        return EKeyWasNotConsumed;
        }

    TInt active = iTabGroup-&gt;ActiveTabIndex();
    TInt count = iTabGroup-&gt;TabCount();

    switch( aKeyEvent.iCode )
        {
        case EKeyLeftArrow:
            if ( active &gt; 0 )
                {
                active--;
                iTabGroup-&gt;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-&gt;TabIdFromIndex( active ) ) );
                }
            break;
        case EKeyRightArrow:
            if( ( active + 1 ) &lt; count )
                {
                active++;
                iTabGroup-&gt;SetActiveTabByIndex( active );
                // ActivateLocalViewL() is used to change the view. 
                // To change view from another application we would use ActivateViewL()
                ActivateLocalViewL( TUid::Uid( iTabGroup-&gt;TabIdFromIndex( active ) ) );
                }
            break;
        default:
            return EKeyWasNotConsumed;
            break;
        }

    return EKeyWasConsumed;
    }
</codeblock>
<p><parmname>CMyAppView</parmname> is a UI controller that is using key
events from the selection key of a mobile device to switch between two <xref href="jar:GUID-759FBC7F-5384-4487-8457-A8D4B76F6AA6.jar!/html/classCAknView.html" format="application/java-archive"><parmname>CAknView</parmname></xref>-derived
views. <parmname>EKeyLeftArrow</parmname> and <parmname>EKeyRightArrow</parmname> are
codes enumerated in <parmname>TKeyCode</parmname> and are contained in <parmname>KeyEvent.iCode</parmname>.</p>
<note>
<p>The above code snippet was taken from the <i>MyView</i> example application
provided in the <parmname>S60 Platform: Platform Application Views</parmname> package
available from <xref href="http://www.forum.nokia.com/info/sw.nokia.com/id/9c5f21fd-2162-42da-aaf6-17a59b943475/S60_Platform_Application_Views_v2_0_en.zip.html" scope="external">Forum Nokia</xref> . </p>
</note>
<p>For more information on <parmname>TKeyCode</parmname>, see <xref href="jar:GUID-35228542-8C95-4849-A73F-2B4F082F0C44.jar!/sdk/doc_source/reference/reference-cpp/Kernel_Architecture_2/e32keys.hGlobals.html#%3a%3aTKeyCode" format="application/java-archive"><parmname>TKeyCode</parmname></xref>.</p>
</conbody></concept>