前提:
public class TestActivity extends Cocos2dxActivity {
@Override
public boolean onKeyDown( int keyCode, KeyEvent event) {// 监听不到
原因:
Called when a key was pressed down and not handled by any of the views inside of the activity. So, for example, key presses while the cursor is inside a TextView will not trigger the event (unless it is a navigation to another object) because TextView handles its own key presses.
If the focused view didn't want this event, this method is called.
处理:
修改
org.cocos2dx.lib.
Cocos2dxGLSurfaceView
原:
@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);
}
}
改:
@Override
public boolean onKeyDown( final int pKeyCode, final KeyEvent pKeyEvent) {
switch (pKeyCode) {
case KeyEvent. KEYCODE_BACK:
return false;
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);
}
}
经测试
可正常监听返回按键
参考:
http://50vip.com/blog.php?i=390