强制打开CEikEdwin关联的虚拟键盘
文章信息
概述
有些开发者希望虚拟键盘能够自动打开,而不是等用户第一次点击编辑框时打开。本文描述了如何通过模拟用户点击操作强制打开CEikEdwin关联的虚拟键盘。
方案
解决本问题的方案是模拟用户点击CEikEdwin的操作。
void CHelloWorldContainer::LaunchVBKL()
{
if(iEdit1!=NULL)
{
TCursorSelection sel = iEdit1->Selection(); // remember the selection
const TInt KBorderWidth = 2;
const TInt KBorderHeight = 2;
TPointerEvent event;
event.iType = TPointerEvent::EButton1Down;
event.iParentPosition = iEdit1->PositionRelativeToScreen()+TPoint(KBorderWidth,KBorderHeight);
event.iPosition = iEdit1->Position()+TPoint(KBorderWidth,KBorderHeight);
iEdit1->HandlePointerEventL(event); // simulate a pointer down event
event.iType = TPointerEvent::EButton1Up;
iEdit1->HandlePointerEventL(event); // and then simulate a pointer up event to complete the touch operation
iEdit1->SetCursorPosL(sel.iAnchorPos, EFalse);
iEdit1->SetCursorPosL(sel.iCursorPos, ETrue); // restore the selection
}
}
注意以上代码在(容器对象的ConstructL里)CEikEdwin对象刚刚构造完成时调用无效,因此如果想在容器对象构造完成后立即显示虚拟键盘的话,应该启动一个活动对象并在RunL()中调用上述方法。
void CHelloWorldContainer::ConstructL(
const TRect& aRect,
const CCoeControl* aParent,
MEikCommandObserver* aCommandObserver )
{
...
// begin by chen
iAsyncCallBack = new(ELeave) CAsyncCallBack(TCallBack(AsyncCallBack, this), CActive::EPriorityStandard);
iAsyncCallBack->CallBack(); // AO that runs only once
// end by chen
}
...
TInt CHelloWorldContainer::AsyncCallBack(TAny* aParam)
{
if(aParam==NULL)
{
return KErrNoMemory;
}
CHelloWorldContainer* self = static_cast<CHelloWorldContainer*>(aParam);
TRAPD(err, self->LaunchVBKL());
return err;
}
完整的示例程序: HelloWorld(LaunchVKB).zip
相关文档 (和讨论):

