Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android back button does not work

I am using cocos2dx to make a small game and in the activity of my game i give the following functions to handle back button.

@Override
 public boolean onKeyDown(int keyCode, KeyEvent event)
 {
     return super.onKeyDown(keyCode, event);
 }

 @Override
 public void onDestroy()
 {
     android.os.Process.killProcess(android.os.Process.myPid());
     super.onDestroy();
 }

On pressing back button i get the following warning in my logcat

Can't open keycharmap file

Error loading keycharmap file '/system/usr/keychars/qtouch-touchscreen.kcm.bin'. hw.keyboards.65538.devname='qtouch-touchscreen'

The call doesn't reach onKeyDown or onDestroy functions.

Please tell me why this warning is caused and why i cannot handle the android back button.

The functions work fine on my java android project but not in my cocos2d-x project

like image 525
glo Avatar asked Dec 04 '12 07:12

glo


5 Answers

It is been handled here in the file Cocos2dxGLSurfaceView.java

change it to below, where myActivity is the cocos2dActicity

        case KeyEvent.KEYCODE_BACK:
                    AlertDialog ad = new AlertDialog.Builder(myActivity)
                    .setTitle("EXIT?")
                    .setMessage("Do you really want to exit?")
                    .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            ((Cocos2dxActivity)myActivity).finish();
                        }
                    })
                    .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }).create();
                    ad.show();
            return true;
        case KeyEvent.KEYCODE_MENU:
like image 132
m.ding Avatar answered Oct 16 '22 10:10

m.ding


To handle back button pressing you need to redefine onBackPressed() method of your activity, not this two methods.

like image 33
Evos Avatar answered Oct 16 '22 12:10

Evos


Have you enabled the touch? If not then please enable it and Hope, it'll sort-out your problem.

I assume that's button in your game screen.

like image 45
Mohit_Jaiswal Avatar answered Oct 16 '22 11:10

Mohit_Jaiswal


Just your apps implements for override method for onKeyDown,

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // Here to implements for your code.
        Log.d(TAG, "KEYCODE_BACK");
    }
    return super.onKeyDown(keyCode, event);
}
like image 38
Najib.Nj Avatar answered Oct 16 '22 10:10

Najib.Nj


Here's an update for Cocos2d-x version 3+

This has been answered simply (and works) here

as well as a slightly less complete youtube here

like image 1
Mike Avatar answered Oct 16 '22 12:10

Mike