CClayer继承了CCKeypadDelegate类,所以我们继承CClayer,来重写其中CCKeypadDelegate的两个函数:
class CC_DLL CCKeypadDelegate
{
public:
// The back key clicked
virtual void keyBackClicked() {}
// The menu key clicked. only available on wophone & android
virtual void keyMenuClicked() {};
};
在Cocos2dx的CCLayer'类中,我们可以看到其中有一个bool m_bKeypadEnabled;变量,此变量用来控制是否接受用户的按键响应,CCLayer中有一个函数:
/// isKeypadEnabled setter
void CCLayer::setKeypadEnabled(bool enabled)
{
if (enabled != m_bKeypadEnabled)
{
m_bKeypadEnabled = enabled;
if (m_bRunning)
{
CCDirector* pDirector = CCDirector::sharedDirector();
if (enabled)
{
pDirector->getKeypadDispatcher()->addDelegate(this);
}
else
{
pDirector->getKeypadDispatcher()->removeDelegate(this);
}
}
}
}
可以看出,在CCDirector类中有一个 CCKeypadDispatcher对象的定义。
/** CCKeypadDispatcher associated with this director
@since v2.0
*/
CC_PROPERTY(CCKeypadDispatcher*,m_pKeypadDispatcher,KeypadDispatcher);