Android back button on Cocos2d-JS v3.0 alpha


#1

Hi,

I wrote the code to detect android back button which worked properly in Cocos2d-x v3.0beta JSBinding.

var layer = cc.Layer.extend({
	onEnterTransitionDidFinish: function() {
		this.setKeypadEnabled(true);
	},

	backClicked: function (){
		// ...
	}
});

But in Cocos2d-JS v3.0 alpha, it won’t work.
Does anyone know how to realize it?

Thanks.


#2

Hi @nekobako,

Please use keyboard event listener to realize keypadEnabled in Cocos2d-JS v3.0.

We remove all switches from cc.Layer, so setKeypadEnabled is invalid.

The new way:

  1. add a keyboard event listener:
var keyboardListener = cc.EventListener.create({
   event: cc.EventListener.KEYBOARD,
   onKeyPressed:  function(keyCode, event){
      if(keyCode == cc.KEY.backspace){
         //do something
      }else if(keyCode == cc.KEY.home){
         //do something
      }
   }
});
  1. add to a layer:

cc.eventManager.addListener(keyboardListener, aLayer);


#3

@ludingping Thanks!
But it won’t work on my devices.

I tested it on Samsung Galaxy S, Sony Xperia Z1 and BlueStacks Player.
(It works on html5 with BackSpace key)

Is there any other way?


#4

I’m sorry,

The keyCode of “home” (36) in H5 doesn’t equal “KEY_HOME” (0x1050) in -x.

A best way is recording the keycode of the pressing back button, and use the code to check the button whether pressed.

  onKeyPressed:  function(keyCode, event){
      cc.log(keyCode);  // record the keycode of back key
   }
  onKeyPressed:  function(keyCode, event){
      if(keyCode == 0x1050){ //the home key
         //do something.
      }
   }

#5

Thanks. But it seems that onKeyPressed event is not firing on android.

onEnterTransitionDidFinish: function() {
	cc.log("key test");
	cc.eventManager.addListener(cc.EventListener.create({
		event: cc.EventListener.KEYBOARD,
		onKeyPressed: function(keyCode, event){
			cc.log("key pressed");
		}
	}), this);
}

LogCat said “key test” when transition finished. Then I pressed back, menu, home volume key, but LogCat never said “key pressed”.

(in -html5, Chrome console said “key pressed” when I use the keyboard.)


#6
for(var key in cc.sys.capabilities) {
	cc.log("cc.sys.capabilities[" + key + "] : " + cc.sys.capabilities[key]);
}

Chrome:
cc.sys.capabilities[canvas] : true
cc.sys.capabilities[opengl] : true
cc.sys.capabilities[mouse] : true
cc.sys.capabilities[keyboard] : true
cc.sys.capabilities[accelerometer] : true

Android:
cc.sys.capabilities[opengl] : true
cc.sys.capabilities[accelerometer] : true
cc.sys.capabilities[touches] : true

Android doesn’t support keyboard events in Cocos2d-JS v3.0, right?


#7

I have the same question, anybody can fix it?


#8

AnyBody fixed??
or it’s a unsovled bug??


#9

http://www.cocos2d-x.org/issues/4546
The issue have been created here, you can watch it so that you can know when it’s solved


#10

@pandamicro thanks!


#11

Hi,
@pandamicro@ludingping
Issue (http://www.cocos2d-x.org/issues/4546) has been closed. But it seems the issue is different. I am using the latest version 3.2. And with the following code:

var keyboardListener = cc.EventListener.create({
event:cc.EventListener.KEYBOARD,
onKeyPressed:function(keyCode, event){
cc.log("pressed Keycode = " + keyCode);
if(keyCode == cc.KEY.back){
cc.director.end();
}
}
});

    cc.eventManager.addListener(keyboardListener, mjLayer);

But the onKeyPressed function is never fired, it is so strange. Is it a bug in V3.2?
And I didn’t find the definition of cc._EventListenerKeyboard, but it exists in V3.0, why it is removed now? In the above post, it says using cc.KEY.backspace, but in the definition, it says ‘back’ is for android. But anyway, neither one is working.

Thanks and wish I could solve it earlier.


#12

Hey @pandamicro or @ludingping,

I’m having the same issue as @jeromeyan . The onKeyPressed function never gets fired on the keyboard event listener (neither on iOS or Android). Do you guys know why this is happening and how to solve it? Thanks in advance ;-).


#13

I have changed onKeyPressed to onKeyReleased and now ‘onKeyReleased’ gets fired in Android. Below is the code

var keyboardListener = cc.EventListener.create({
event:cc.EventListener.KEYBOARD,
onKeyReleased:function(keyCode, event){
cc.log("pressed Keycode = " + keyCode);
if(keyCode == 6){
cc.director.end();

}
}
});