How to block the back key in android when using Qt

匿名 (未验证) 提交于 2019-12-03 00:48:01

问题:

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!     ... } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!