Search code examples
androidcocos2d-xcocos2d-x-3.0

cocos2dx onKeyReleased gets called at keydown instead of keyup in android


I am recently working with cocos2dx 3.0rc2 and I want to respond the back key event in android platform. I learned that I could override the onKeyReleased method of the layer and setKeypadEnabled(true) to capture the back key.

However I got a problem, I can capture the event, but not accurately. That is, I expect this method gets called when I released my finger from the back key. However it now gets triggered as soon as I put my finger on the back key. That being said, it responds at key-down phase while I wish it could do it at key-up phase.

Can you help me with this? By the way, I tested the code and it seemed that on win32, the backspace key was not responded (But it does not matter for me as I only care about android)

Here are the code blocks:

init:

...
this->setKeypadEnabled(true);
...

onKeyReleased:

...
if(keyCode ==  EventKeyboard::KeyCode::KEY_BACKSPACE) {
    onBack(nullptr);
}
...

I also tried the other way to capture the event, by setting the listener instead of simply putting setKeypadEnabled(true). The result is the same.

I appreciate your help!


Solution

  • You have written the correct code but have used the wrong key code.

    It shoud be EventKeyboard::KeyCode::KEY_ESCAPE.