| ID | CS000868 | Creation date | March 28, 2008 |
| Platform | S60 3rd Edition, FP1 | Tested on devices | Nokia N95 |
| Category | Symbian C++ | Subcategory | UI |
| Keywords (APIs, classes, methods, functions): CCoeControl, TKeyEvent |
This code snippet shows how to add key event handling to the custom container control and how change focus of the selected component.
This example extends the existing code snippet CS000861 - Custom control: Container control. Check the See also section of this article for other snippets of the Custom Control series.
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();
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);
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();
}
}
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().
CMyContainerControl receives key events and shows active component focus.
Custom Control Series:
No related wiki articles found