From fc98d027c00aeff0dc8062492683e4952126bb1b Mon Sep 17 00:00:00 2001 From: Paul Olav Tvete Date: Tue, 23 Apr 2013 10:47:55 +0200 Subject: [PATCH] Android: handle keyPress event for Key_Back Added logic so that accepting either the press or the release will keep the app running. This makes it possible to use the onBackPressed functionality in QML. This functionality is only intended for running in a complete Android environment, so make sure that we don't terminate the application in the NO_SDK case. Task-number: QTBUG-30803 Change-Id: I2546eea73bf6a6ee8b196125b7556479b9b10a9c Reviewed-by: BogDan Vatra --- src/gui/kernel/qguiapplication.cpp | 43 +++++++++++++----------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index d254f7c9bc..2e6000625e 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -1666,41 +1666,36 @@ void QGuiApplicationPrivate::processKeyEvent(QWindowSystemInterfacePrivate::KeyE QWindow *window = e->window.data(); modifier_buttons = e->modifiers; if (e->nullWindow -#ifdef Q_OS_ANDROID - || (e->keyType == QEvent::KeyRelease && e->key == Qt::Key_Back) || e->key == Qt::Key_Menu +#if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_NO_SDK) + || e->key == Qt::Key_Back || e->key == Qt::Key_Menu #endif ) { window = QGuiApplication::focusWindow(); } - if (!window -#ifdef Q_OS_ANDROID - && e->keyType != QEvent::KeyRelease && e->key != Qt::Key_Back -#endif - ) { - return; - } - if (window && window->d_func()->blockedByModalWindow) { - // a modal window is blocking this window, don't allow key events through - return; - } QKeyEvent ev(e->keyType, e->key, e->modifiers, e->nativeScanCode, e->nativeVirtualKey, e->nativeModifiers, e->unicode, e->repeat, e->repeatCount); ev.setTimestamp(e->timestamp); -#ifdef Q_OS_ANDROID - if (e->keyType == QEvent::KeyRelease && e->key == Qt::Key_Back) { - if (!window) { - qApp->quit(); - } else { - QGuiApplication::sendEvent(window, &ev); - if (!ev.isAccepted() && e->key == Qt::Key_Back) - QWindowSystemInterface::handleCloseEvent(window); - } - } else -#endif + // only deliver key events when we have a window, and no modal window is blocking this window + + if (window && !window->d_func()->blockedByModalWindow) QGuiApplication::sendSpontaneousEvent(window, &ev); +#if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_NO_SDK) + else + ev.setAccepted(false); + + static bool backKeyPressAccepted = false; + if (e->keyType == QEvent::KeyPress) { + backKeyPressAccepted = e->key == Qt::Key_Back && ev.isAccepted(); + } else if (e->keyType == QEvent::KeyRelease && e->key == Qt::Key_Back && !backKeyPressAccepted && !ev.isAccepted()) { + if (!window) + qApp->quit(); + else + QWindowSystemInterface::handleCloseEvent(window); + } +#endif } void QGuiApplicationPrivate::processEnterEvent(QWindowSystemInterfacePrivate::EnterEvent *e)