naive231
naive231

Reputation: 1380

Get cocos2d-x keyboard info on android

I simply insert following codes in a clean base(at the final of HelloWorld::init()) produced by cocos new:

auto kl = EventListenerKeyboard::create();

kl->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event) {
    CCLOG("%s> keyCode=%d",__FUNCTION__,keyCode);
};
auto ed = Director::getInstance()->getEventDispatcher();
ed->addEventListenerWithSceneGraphPriority(kl,this);

On windows, it worked well. But nothing happened on android. Log with value of keyCode should be left in logcat, I think. Do I miss anything?

Upvotes: 2

Views: 1303

Answers (1)

Emadpres
Emadpres

Reputation: 3737

Approach One: If you just need to handle keyboard as key-event, It's as easy as these below lines of code:

HelloWorld::init()
{ 
    ...
    auto keyboardListener = EventListenerKeyboard::create();
    keyboardListener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event)
    {
        switch (keyCode)
        {
        case EventKeyboard::KeyCode::KEY_UP_ARROW:      /*Jump maybe*/  break;
        case EventKeyboard::KeyCode::KEY_DOWN_ARROW:    /*Crouch maybe*/    break;
        case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:   /*Move Right maybe*/    break;
        case EventKeyboard::KeyCode::KEY_LEFT_ARROW:    /*Move Left maybe*/ break;
        }
    };
    _eventDispatcher->addEventListenerWithSceneGraphPriority(keyboardListener, this);
    ...
return true;
}

I think it's clear enough not to need any extra description.

Approach Two: if you need an input box that user/player can enter string with keyboard and you get what is entered, I recommend to use TextField which is available in cocos2d v3 ( and with some difficulty in v2) and has a full functionality. You can create and initial one of them as:

auto textField = cocos2d::ui::TextField::create("hint: enter here","Arial" , 30);
textField->setTextHorizontalAlignment(cocos2d::TextHAlignment::CENTER);
textField->setTextVerticalAlignment(cocos2d::TextVAlignment::CENTER);
textField->setColor(Color3B(100,100,100));
textField->setMaxLength(10);
textField->setMaxLengthEnabled(true);
textField->setTouchAreaEnabled(true);
textField->setTouchSize(Size(200,400));
textField->setPosition(...);
textField->addEventListener(CC_CALLBACK_2(HelloWorld::textFieldEvent, this));
this->addChild(textField, 10);

You can get entered data any time with std::string enteredData= textField->getString();

You can also do something when user entering text with two event as :

void HelloWorld::textFieldEvent(Ref *pSender, cocos2d::ui::TextField::EventType type)
{
    switch (type)
    {
    case cocos2d::ui::TextField::EventType::ATTACH_WITH_IME:
    {
        textField->setColor(Color3B::BLACK);
        // or whatever elese
        break;
    }
    case cocos2d::ui::TextField::EventType::DETACH_WITH_IME:
    {
        textField->setColor(Color3B(100,100,100));
        // or whatever elese
        break;
    }
    }
}

Enjoy !

Upvotes: 1

Related Questions