Attach keyboard to textfield when a button is tapped

Hi,

I have a textfield which opens keypad when tapped anywhere on the screen, but i want to open the keypad when a different button is tapped. Button name is “editbtn”…

when i add editbtn like this it gives an error -

_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, editbtn);

so what should i modify in the code -

email = cocos2d::TextFieldTTF::textFieldWithPlaceHolder("hello", "fonts/A little sunshine.ttf", 48);
cocos2d::TTFConfig config = email->getTTFConfig();
config.fontFilePath = "fonts/A little sunshine.ttf";
config.fontSize = 48;
email->setTTFConfig(config);

 email->setPosition(Vec2(button4->getPositionX(),
                        button4->getPositionY()));

email->setColorSpaceHolder(Color3B(255,255,255));
email->setTextColor(Color4B(255,255,255,255));

email->setColor(Color3B(255,255,255));

email->setDelegate(this);
email->setVisible(false);
this->addChild(email);


auto touchListener = EventListenerTouchOneByOne::create();

touchListener->onTouchBegan = [](cocos2d::Touch* touch, cocos2d::Event * event)->bool {
    try {
        // Show the on screen keyboard
        auto email= dynamic_cast<TextFieldTTF *>(event->getCurrentTarget());
        email->attachWithIME();
        return true;
    }
    catch(std::bad_cast & err){
        return true;
    }
};

    _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, email);

button code is -

edit = MenuItemImage::create(
                             "editbtn.png",
                             "CloseSelected.png",
                             CC_CALLBACK_1(TScene::editclicked, this));

edit->setPosition(Point(visibleSize.width/2 + origin.x , visibleSize.height/2 + origin.y);

// create menu, it's an autorelease object
editbtn = Menu::create(edit, NULL);
editbtn->setPosition(Point::ZERO);
this->addChild(editbtn);

Hi @mihir77,

I was checking your code.

While you are creating Menu Button , you have added callback function with the name editclicked.
Where is the implementation method of editclicked function?

i got it working by moving the code to editclicked -

void TimerScene::editclicked(cocos2d::Ref *sender)
{
 Size visibleSize = Director::getInstance()->getVisibleSize();
 Vec2 origin = Director::getInstance()->getVisibleOrigin();


auto touchListener = EventListenerTouchOneByOne::create();

touchListener->onTouchBegan = [](cocos2d::Touch* touch, cocos2d::Event * event)->bool {
    try {
        
        auto email = dynamic_cast<TextFieldTTF *>(event->getCurrentTarget());
        email->attachWithIME();
        return true;
    }
    catch(std::bad_cast & err){
        return true;
    }
};

_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, email);
}

so when i tap the edit button the keypad is attached and i can edit the textfield, but after that whenever i touch anywhere on the screen keypad shows up…

so how to open the keypad only when button is tapped and not when touched anywhere on the scene?

Hi @mihir77,

I can see you have added touch event listener inside editclicked clicked callback function.
What is the need to touch event listener here?

From your requirement i can see keypad need to open when button is tapped. Just remove the touch event inside editclicked function and test once.

void TimerScene::editclicked(cocos2d::Ref *sender)
{
 Size visibleSize = Director::getInstance()->getVisibleSize();
 Vec2 origin = Director::getInstance()->getVisibleOrigin();
 auto email = dynamic_cast<TextFieldTTF *>(event->getCurrentTarget());
 email->attachWithIME();
 return true;
}

oh yes there is no need for touch event listener, removed it and it worked, thanks… :smiley:

but just for learning, by using touch event i want to open keypad,(i.e by touching the textfield) what should i modify, as in my code, if i touch anywhere the keypad pops up…

Hi,

Create Text field using:

TextField* textField = TextField::create("input words here","Arial",30);

        textField->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
        textField->addEventListener(CC_CALLBACK_2(UITextFieldTest::textFieldEvent, this));
        this->addChild(textField);
1 Like

@mihir77: Were you successful with hiding the keypad when tapping on the other area on the screen ? I’m badly in need of solution. I also needed to open the keypad when tapping on a button (MenuItemImage) . I’m still unable make the keypad go away when tapping on the other screen area.
@Gurudath: I was doing the same way you posted. Could you please help me on this. The keypad doesn’t go away when tapping on other area if it’s open by textField->openWithIME() .
Sorry for starting the old conversation again but I’m very badly stuck in here.

@jeevs,

Have you tried by calling textField->closeIME(); method?

@Gurudath: Thanks for your input but this method is not available. I’m using version 3.13.1.

Hi,

Am using cocos2dx V3.12.

I have created TextField:
textField = TextField::create(“input words here”,“Arial”,30);

When i click on any button am calling:
textField->attachWithIME();

After that, when i click on other area automatically keypad gets hides.
When i checked case TextField::EventType::DETACH_WITH_IME: is also getting called.