From a28ad6939de2f9006eb142339bbc71634ea4f2ee Mon Sep 17 00:00:00 2001 From: WenhaiLin Date: Thu, 2 Jul 2015 16:19:33 +0800 Subject: [PATCH] Fixed EventListenerKeyboard.onKeyPressed not firing for back button on Android. --- .../cocos2dx/lib/Cocos2dxGLSurfaceView.java | 24 +++++++++++++++++++ .../org/cocos2dx/lib/Cocos2dxRenderer.java | 8 +++++-- cocos/platform/android/jni/TouchesJni.cpp | 4 ++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java index f4139193e1..a908222d52 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java @@ -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 // =========================================================== diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java index 7847b4c035..83ddd8e7cc 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java @@ -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 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 boolean nativeKeyDown(final int keyCode); + private static native boolean nativeKeyEvent(final int keyCode,boolean isPressed); private static native void nativeRender(); private static native void nativeInit(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) { - Cocos2dxRenderer.nativeKeyDown(keyCode); + Cocos2dxRenderer.nativeKeyEvent(keyCode, true); + } + + public void handleKeyUp(final int keyCode) { + Cocos2dxRenderer.nativeKeyEvent(keyCode, false); } public void handleOnPause() { diff --git a/cocos/platform/android/jni/TouchesJni.cpp b/cocos/platform/android/jni/TouchesJni.cpp index b3a74ebdd1..c25d6427a3 100644 --- a/cocos/platform/android/jni/TouchesJni.cpp +++ b/cocos/platform/android/jni/TouchesJni.cpp @@ -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(); auto iterKeyCode = g_keyCodeMap.find(keyCode); @@ -109,7 +109,7 @@ extern "C" { } cocos2d::EventKeyboard::KeyCode cocos2dKey = g_keyCodeMap.at(keyCode); - cocos2d::EventKeyboard event(cocos2dKey, false); + cocos2d::EventKeyboard event(cocos2dKey, isPressed); cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); return JNI_TRUE;