×
Namespaces

Variants
Actions
Revision as of 07:32, 30 May 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Changing the focus of a Symbian custom control

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Code ExampleTested with
Devices(s): Nokia N95
CompatibilityArticle
Keywords: CCoeControl, TKeyEvent
Created: tepaa (10 Mar 2008)
Last edited: hamishwillee (30 May 2013)

Contents

Overview

This code snippet shows how to add key event handling to the custom container control and how change focus of the selected component. The example extends the existing code snippet Constructing a Symbian container control. Check the SeeAlso section of this article for other snippets of the Custom Control series.

CMyContainerControl - Header

Add three methods to the CMyContainerControl component header for receiving key events and changing focus.

private:
TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType);
public:
void MoveFocusUp();
void MoveFocusDown();

CMultiViewsView1 - Source

CMyContainerControl must be added to the control stack for receiving key events AppUi()->AddToStackL().

// Create Compound control
iContainerControl = CMyContainerControl::NewL(ClientRect());
 
// Create control to the compound container
CMyControl* control1 = CMyControl::NewLC(ClientRect(),iContainerControl);
iContainerControl->AddControlL(control1,1);
CleanupStack::Pop(); //control 1
 
// Add the control to the control stack
AppUi()->AddToStackL(iContainerControl);

CMyContainerControl - Source

OfferKeyEventL() catches key events EKeyUpArrow and EKeyDownArrow for changing focus of the selected component. MoveFocusUp() and MoveFocusDown() changes the active component focus to another one with CCoeContainer::SetFocus().

TKeyResponse CMyContainerControl::OfferKeyEventL
(const TKeyEvent& aKeyEvent,TEventCode /*aType*/)
{
// Move focus
switch ( aKeyEvent.iCode )
{
case EKeyUpArrow:
{
MoveFocusUp();
DrawNow(); // Draw components again
return EKeyWasConsumed;
}
case EKeyDownArrow:
{
MoveFocusDown();
DrawNow(); // Draw components again
return EKeyWasConsumed;
}
default:
{
return EKeyWasNotConsumed;
}
}
}
 
void CMyContainerControl::MoveFocusUp()
{
CCoeControlArray::TCursor cursor = Components().Begin();
CCoeControl* ctrl = NULL;
CCoeControl* prevCtrl = NULL;
while ((ctrl = cursor.Control<CCoeControl>()) != NULL)
{
if (ctrl->IsFocused())
{
if (prevCtrl)
{
// Set focus to previous control
ctrl->SetFocus(EFalse);
prevCtrl->SetFocus(ETrue);
break;
}
else
{
break; // First control is already focused
}
}
prevCtrl = ctrl;
cursor.Next();
}
}
 
void CMyContainerControl::MoveFocusDown()
{
CCoeControlArray::TCursor cursor = Components().Begin();
CCoeControl* ctrl = NULL;
CCoeControl* nextCtrl = NULL;
while ((ctrl = cursor.Control<CCoeControl>()) != NULL)
{
if (ctrl->IsFocused())
{
cursor.Next();
nextCtrl = cursor.Control<CCoeControl>();
if (nextCtrl)
{
// Set focus to next control
ctrl->SetFocus(EFalse);
nextCtrl->SetFocus(ETrue);
break;
}
else
{
break; // Last control is already focused
}
}
cursor.Next();
}
}

Drawing focus

Focus rectangle is drawn in the custom control CMyControl::Draw() method which calls CMyControl::DrawFocusFrame() that checks component focus. If the component has focus, it is drawn with CWindowGc::DrawRoundRect().


Postconditions

CMyContainerControl receives key events and shows active component focus.

See also

Custom Control Series:

This page was last modified on 30 May 2013, at 07:32.
87 page views in the last 30 days.
×