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;