Fixed EventListenerKeyboard.onKeyPressed not firing for back button on Android.

This commit is contained in:
WenhaiLin 2015-07-02 16:19:33 +08:00
parent 2774d6ff1d
commit a28ad6939d
3 changed files with 32 additions and 4 deletions

View File

@ -313,6 +313,30 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView {
} }
} }
@Override
public boolean onKeyUp(final int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
case KeyEvent.KEYCODE_MENU:
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
case KeyEvent.KEYCODE_DPAD_CENTER:
this.queueEvent(new Runnable() {
@Override
public void run() {
Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleKeyUp(keyCode);
}
});
return true;
default:
return super.onKeyUp(keyCode, event);
}
}
// =========================================================== // ===========================================================
// Methods // Methods
// =========================================================== // ===========================================================

View File

@ -115,7 +115,7 @@ public class Cocos2dxRenderer implements GLSurfaceView.Renderer {
private static native void nativeTouchesEnd(final int id, final float x, final float y); private static native void nativeTouchesEnd(final int id, final float x, final float y);
private static native void nativeTouchesMove(final int[] ids, final float[] xs, final float[] ys); private static native void nativeTouchesMove(final int[] ids, final float[] xs, final float[] ys);
private static native void nativeTouchesCancel(final int[] ids, final float[] xs, final float[] ys); private static native void nativeTouchesCancel(final int[] ids, final float[] xs, final float[] ys);
private static native boolean nativeKeyDown(final int keyCode); private static native boolean nativeKeyEvent(final int keyCode,boolean isPressed);
private static native void nativeRender(); private static native void nativeRender();
private static native void nativeInit(final int width, final int height); private static native void nativeInit(final int width, final int height);
private static native void nativeOnSurfaceChanged(final int width, final int height); private static native void nativeOnSurfaceChanged(final int width, final int height);
@ -139,7 +139,11 @@ public class Cocos2dxRenderer implements GLSurfaceView.Renderer {
} }
public void handleKeyDown(final int keyCode) { public void handleKeyDown(final int keyCode) {
Cocos2dxRenderer.nativeKeyDown(keyCode); Cocos2dxRenderer.nativeKeyEvent(keyCode, true);
}
public void handleKeyUp(final int keyCode) {
Cocos2dxRenderer.nativeKeyEvent(keyCode, false);
} }
public void handleOnPause() { public void handleOnPause() {

View File

@ -100,7 +100,7 @@ extern "C" {
}; };
JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeKeyDown(JNIEnv * env, jobject thiz, jint keyCode) { JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeKeyEvent(JNIEnv * env, jobject thiz, jint keyCode, jboolean isPressed) {
Director* pDirector = Director::getInstance(); Director* pDirector = Director::getInstance();
auto iterKeyCode = g_keyCodeMap.find(keyCode); auto iterKeyCode = g_keyCodeMap.find(keyCode);
@ -109,7 +109,7 @@ extern "C" {
} }
cocos2d::EventKeyboard::KeyCode cocos2dKey = g_keyCodeMap.at(keyCode); cocos2d::EventKeyboard::KeyCode cocos2dKey = g_keyCodeMap.at(keyCode);
cocos2d::EventKeyboard event(cocos2dKey, false); cocos2d::EventKeyboard event(cocos2dKey, isPressed);
cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
return JNI_TRUE; return JNI_TRUE;