Android Back button, onKeyReleased getting called repeatedly

Hi there [Cocos2D-x v3.4 Final],

I’m having some issues with the android back button, when it is pressed on a device the virtual function onKeyReleased is constantly getting called. This is not how it should function and it does function correctly when using a physical PC keyboard for example. When you press and hold the escape key, the function doesn’t get called. As soon as you release it, it gets called.

At first I thought that the actual android hardware may not be able to detect press and release on the device back button and only detects a touch. But I have found the correct functionality working in other apps. I’ve tested this on many different devices.

I believe this is an issue with cocos framework and the keyboard listener itself, I would really like if this worked correctly. If anyone has any ideas on how to fix it without waiting for a fix on an updated version of cocos please point me in the right direction.

Well, you could debug into the cocos/java code to see where it comes from…
I just assumed that it’s send like this by android itself (I don’t know this for sure).

Due to my setup, I already put all input event in a queue. And I just set the “pressed” flag to true when I catch the key in the on key released function on Android.

You could do something similar… when you receive a key back event on Android, you could trigger the on key pressed event for the key yourself.

Kind regards,
Michaël

I haven’t had much luck getting the gdb debugger to work on console in windows, so that option is out the window. It’s definitely not how the button functions, there is 100% touch and release functionality on the back button. So unless the cocos developers have had to restrict the use of that functionality for some reason or another, then I don’t know.

Right okay, but where would you know where to unset the “pressed” flag? I’m struggling to see how you can recreate the correct functionality of the button if the button isn’t working as expected itself. Because essentially what is happening is as soon as the button is touched, OnKeyTouched is called and then OnKeyReleased is called immediately after and then fired repeatedly until it is released. I’m struggling to see how you could determine when to turn off the flag that the key is no longer touched, unless you had some sort of timer or something, to say key hasn’t been touched for 0.5 seconds or something. But that sounds really hacky.

Regards,
Mark

Is above problem solved? Please tell me I am also having the same issue in my game.

if anybody still struggling with the implementation for backButton (I checked on Android), refer to following link:

In short use following code:

In the Layer’s init function:
this->setKeyboardEnabled(true);

Then the onKeyReleased definition:
void YourLayer::onKeyReleased(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event * event)
{
if (keyCode == cocos2d::EventKeyboard::KeyCode::KEY_BACK){
/* Your code here. */
}
}