See http://www.haogongju.net/art/1908278
1. Modify TouchesJni in the cocos2dx/platform/android/jni directory. in the cpp file, add the key value you want to process. The value must be consistent with the corresponding Android key value. The following is an example of the return button and menu button. You can add the desired key value:
# Define KEYCODE_BACK 0x04
# Define KEYCODE_MENU 0x52
# Define KEYCODE_DPAD_UP 0x00000013 // mount the key
# Define KEYCODE_DPAD_DOWN 0x00000014 // bottom key
# Define KEYCODE_DPAD_LEFT 0x00000015 // left button
# Define KEYCODE_DPAD_RIGHT 0x00000016 // right-click
# Define KEYCODE_DPAD_CENTER 0x00000017 // confirm the key
# Define KEYCODE_HOME 0x00000003 // Home Key
# Define KEYCODE_POWER 0x0000001a // Power Supply key
# Define KEYCODE_VOLUME_DOWN 0x00000019 // volume reduction
# Define KEYCODE_VOLUME_UP 0x00000018 // volume increase
# Define KEYCODE_CAMERA 0x0000001b // camera
2. Modify the jboolean Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeKeyDown (JNIEnv * env, jobject thiz, jint keyCode) function under the file as follows:
Jboolean Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeKeyDown (JNIEnv * env, jobject thiz, jint keyCode)
{
CCDirector * pDirector = CCDirector: shareddire ();
Switch (keyCode ){
Case KEYCODE_BACK:
If (pDirector-> getKeypadDispatcher ()-> dispatchKeypadMSG (kTypeBackClicked ))
Return jni_true;
Break;
Case keycode_menu:
If
(Pdirector-> getkeypaddispatcher ()-> dispatchkeypadmsg (ktypemenuclicked ))
Return jni_true;
Break;
Default:
Return jni_false;
}
Return jni_false;
}
}
The corresponding keycode is the key value waiting for processing.
3. Modify the cckeypaddispatcher. h file in the cocos2dx/keypad_dispatcher directory as follows:
Typedef Enum {
// The back key clicked msg
Ktypebackclicked = 1,
Ktypemenuclicked,
} Cckeypadmsgtype;
The message types ktypebackclicked and ktypemenuclicked are the Declaration of the Message Type in step 2.
4. Modify the corresponding CPP file cckeypaddispatcher. cpp, modify the function dispatchkeypadmsg (cckeypadmsgtype nmsgtype), and add the message type you want to process. Here we will use the return and menu keys for example:
If (m_pdelegates-> count ()> 0)
{
Ccobject * pobj = NULL;
Ccarray_foreach (m_pdelegates, pobj)
{
Cc_break_if (! Pobj );
Phandler = (cckeypadhandler *) pobj;
Pdelegate = phandler-> getdelegate ();
Switch (nmsgtype)
{
Case ktypebackclicked:
Pdelegate-> keybackclicked ();
Break;
Case ktypemenuclicked:
Pdelegate-> keymenuclicked ();
Break;
Default:
Break;
}
}
}
The switch statement processes custom message types.
5. Modify the CCKeypadDelegate. h file in the current directory and add the following interface to CC_DLL CCKeypadDelegate, which is the response function of the corresponding message:
Class CC_DLL CCKeypadDelegate
{
Public:
// The back key clicked
Virtual void keyBackClicked (){}
// The menu key clicked. only available on wophone & android
Virtual void keyMenuClicked (){};
};
6. Implement the above interface in the class to be responded.
7. Modify the cocos2dx\ platform \ android \ java \ src \ org \ cocos2dx \ lib Cocos2dxGLSurfaceView. java File
@ Override
Public boolean onKeyDown (final int pKeyCode, final KeyEvent pKeyEvent ){
Switch (pKeyCode ){
Case KeyEvent. KEYCODE_BACK:
Case KeyEvent. KEYCODE_MENU:
This. queueEvent (new Runnable (){
@ Override
Public void run (){
Cocos2dxGLSurfaceView. this. mCocos2dxRenderer. handleKeyDown (pKeyCode );
}
});
Return true;
Default:
Return super. onKeyDown (pKeyCode, pKeyEvent );
}
}
Note:
1. Follow the Back and Menu keys to add the buttons you want to process. In addition, if org \ cocos2dx \ lib has been copied to your android project directory, modify the buttons accordingly;
2. After the modification of the windows version needs to re-compile the cocos2d-x source code, run the cocos2d-x installation directory under the build-win32.bat can; android version or with cygwin compilation can, cygwin will re-compile the modified source code;
3. Add the response function of the message to your Layer class, such as virtual void keyBackClicked (). You can implement the function based on your own needs. However, setKeypadEnabled (true) is required before use );
The above are my own summaries. If you have any shortcomings or mistakes, please correct them!