How to lock rear key in android when using Qt

I need the im application to do so to exit if someone clicks on the Android button on Android, so I can send a message to ask if the user wants to leave the application or not, I found that using:

@Override void MainWindow::onBackPressed() { ... } 

I could handle this event, I tried it in my requireitas project, and that didn't work. Can qtkeyevent handle this? or is there any other way to do this?

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 record the event in Qt in order to send a message

+6
source share
3 answers

Here's 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:

 void MainWindow::keyPressEvent(QKeyEvent *k) { if( k->key() == Qt::Key_MediaPrevious ) { int ret = messageBox( QMessageBox::Yes | QMessageBox::No, "¿Desea salir de la aplicación?", QMessageBox::Question); switch(ret) { case QMessageBox::Yes: this->close(); break; case QMessageBox::No: break; } } } 

Thanks Koying by posting this solution here http://groups.google.com/group/android-qt/browse_thread/thread/676c24e94bb9a200?pli=1

+4
source

You can write onKeyListener for Activity, check for key codes, and if they are what you want, consume them. At least in Java. Of course, C ++ does not have less features.

0
source

Not quite sure if this applies to your situation, but in Android in general, in every Activity interest, you would use the following to override the back button:

 @Override public void onBackPressed() { // Make sure to NOT call super! ... } 
0
source

Source: https://habr.com/ru/post/908097/


All Articles