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
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:
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 to Koying that post this solution here http://groups.google.com/group/android-qt/browse_thread/thread/676c24e94bb9a200?pli=1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With