Problems with back button in device

Problems with back button in device
0

#1

Hello! I’m having problems with systemEvent and the cc.KEY.back, this is the code:

cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, function(){
        switch(event.keyCode) {
            case cc.KEY.back:
                if(self.node.parent.getChildByName("popupExit").active == true){
                    cc.director.resume();
                    self.node.parent.getChildByName("popupExit").runAction(cc.sequence(cc.scaleTo(0.12, 0.5), cc.callFunc(function(){
                        self.node.parent.getChildByName("popupExit").active = false;
                        self.layout.active = false;
                    },self)));
                    self.node.parent.getChildByName("popupExit").runAction(cc.fadeOut(0.12));
                    self.layout.runAction(cc.fadeOut(0.12));
                }else{
                    self.node.parent.getChildByName("popupExit").active = true;
                    self.node.parent.getChildByName("popupExit").opacity = 255*0.2;
                    self.node.parent.getChildByName("popupExit").runAction(cc.sequence(cc.scaleTo(0.10, 0.96), cc.scaleTo(0.02, 1), cc.callFunc(function(){
                        cc.director.pause();
                    },self)));
                    self.node.parent.getChildByName("popupExit").runAction(cc.fadeIn(0.12));
                    self.layout.active = true;
                    self.layout.runAction(cc.fadeTo(0.12, 130));
                }   
            break;
            default:
            break;
        }
            
    }, this);

So, when I use cc.KEY.escape instead cc.KEY.back, it works in web version but using “back” doesn’t do anything on mobile version. Am I doing something wrong?

Many thanks!


#2

Hi,

mabe it’s already (or will be) depreciated, but I use the following piece of code to handle the back key on Android devices:

        cc.eventManager.addListener({
            event: cc.EventListener.KEYBOARD,
            onKeyPressed: function(keyCode, event) {
                if ((keyCode == cc.KEY.back) || (keyCode == cc.KEY.backspace))
                    // Place your code here...
            }.bind(this)
        }, this.node); 

Best regards,
Zsolt


#3

Hey, thanks for your help, I’ve been using the eventManager in the latest versions, but, since it will be deprecated, I’m trying to use an other method. If I don’t find any solution I will use eventManager instead of SystemEvent.

Many Thanks :smiley:


#4

Same problem, In version 2.0.x, cc.eventManager is removed but cc.systemEvent.on seem not work when press back button.