可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I need to prevent the app im doing to exit if someone pushes the back key on an Android device so I can send a messagebox to ask if the user wants to leave the app or not, I found that using:
@Override void MainWindow::onBackPressed() { ... }
I could handle that event, I tried it on my necessitas project and it didn't work. Can qtkeyevent handle this? or is there another way to do it?
I block it using this:
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ( (keyCode == KeyEvent.KEYCODE_BACK) ) { //moveTaskToBack(true); return true; } if (QtApplication.m_delegateObject != null && QtApplication.onKeyDown != null) return (Boolean) QtApplication.invokeDelegateMethod(QtApplication.onKeyDown, keyCode, event); else return super.onKeyDown(keyCode, event); }
Now I need to capture the event on Qt so I an send a message
回答1:
Here is how to solve it:
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { int newKeyCode = keyCode; if ( (keyCode == KeyEvent.KEYCODE_BACK) ) { newKeyCode = KeyEvent.KEYCODE_MEDIA_PREVIOUS; } if (QtApplication.m_delegateObject != null && QtApplication.onKeyDown != null) return (Boolean) QtApplication.invokeDelegateMethod(QtApplication.onKeyDown, newKeyCode, event); else return super.onKeyDown(newKeyCode, event); } public boolean super_onKeyDown(int keyCode, KeyEvent event) { return super.onKeyDown(keyCode, event); } //--------------------------------------------------------------------------- @Override public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { int newKeyCode = keyCode; if ( (keyCode == KeyEvent.KEYCODE_BACK) ) { newKeyCode = KeyEvent.KEYCODE_MEDIA_PREVIOUS; } if (QtApplication.m_delegateObject != null && QtApplication.onKeyMultiple != null) return (Boolean) QtApplication.invokeDelegateMethod(QtApplication.onKeyMultiple ,newKeyCode, repeatCount, event); else return super.onKeyMultiple(newKeyCode, repeatCount, event); } public boolean super_onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { return super.onKeyMultiple(keyCode, repeatCount, event); } //--------------------------------------------------------------------------- @Override public boolean onKeyUp(int keyCode, KeyEvent event) { int newKeyCode = keyCode; if ( (keyCode == KeyEvent.KEYCODE_BACK) ) { newKeyCode = KeyEvent.KEYCODE_MEDIA_PREVIOUS; } if (QtApplication.m_delegateObject != null && QtApplication.onKeyDown != null) return (Boolean) QtApplication.invokeDelegateMethod(QtApplication.onKeyUp, newKeyCode, event); else return super.onKeyUp(newKeyCode, event); } public boolean super_onKeyUp(int keyCode, KeyEvent event) { return super.onKeyUp(keyCode, event); }
and then in Qt:
Thanks to Koying that post this solution here http://groups.google.com/group/android-qt/browse_thread/thread/676c24e94bb9a200?pli=1
回答2:
You can write onKeyListener for the Activity, check for keycodes and if they are what you want, consume them. Al least, in Java. Surely, C++ doesn't have less possibilities.
回答3:
Not quite sure if this applies to your situation, but in Android generally, in each Activity
of interest you would use the following to override the back button press:
@Override public void onBackPressed() { // Make sure to NOT call super! ... }