From e32845b137834ef46d68345a0029d4af7c1d85bb Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Wed, 15 Jan 2014 17:55:48 +0100 Subject: QQuickWindow: Make sure popups receive key events on Mac Change-Id: Ifaf96e567e735d9a837eaef798061bf08fc4a752 Reviewed-by: Jens Bache-Wiig Reviewed-by: Alan Alpert --- src/quick/items/qquickwindow.cpp | 41 +++++++++++++++++++++++++++++----------- src/quick/items/qquickwindow_p.h | 2 ++ 2 files changed, 32 insertions(+), 11 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickwindow.cpp b/src/quick/items/qquickwindow.cpp index c3f5169804..c57ca5e838 100644 --- a/src/quick/items/qquickwindow.cpp +++ b/src/quick/items/qquickwindow.cpp @@ -1299,24 +1299,43 @@ bool QQuickWindow::event(QEvent *e) void QQuickWindow::keyPressEvent(QKeyEvent *e) { Q_D(QQuickWindow); - -#ifndef QT_NO_SHORTCUT - // Try looking for a Shortcut before sending key events - if (QGuiApplicationPrivate::instance()->shortcutMap.tryShortcutEvent(focusObject(), e)) - return; -#endif - - if (d->activeFocusItem) - sendEvent(d->activeFocusItem, e); + d->deliverKeyEvent(e); } /*! \reimp */ void QQuickWindow::keyReleaseEvent(QKeyEvent *e) { Q_D(QQuickWindow); + d->deliverKeyEvent(e); +} - if (d->activeFocusItem) - sendEvent(d->activeFocusItem, e); +void QQuickWindowPrivate::deliverKeyEvent(QKeyEvent *e) +{ + Q_Q(QQuickWindow); + +#ifndef QT_NO_SHORTCUT + // Try looking for a Shortcut before sending key events + if (e->type() == QEvent::KeyPress + && QGuiApplicationPrivate::instance()->shortcutMap.tryShortcutEvent(q->focusObject(), e)) + return; +#endif + + if (activeFocusItem) + q->sendEvent(activeFocusItem, e); +#ifdef Q_OS_MAC + else { + // This is the case for popup windows on Mac, where popup windows get focus + // in Qt (after exposure) but they are not "key windows" in the Cocoa sense. + // Therefore, the will never receive key events from Cocoa. Instead, the + // toplevel non-popup window (the application current "key window") will + // receive them. (QWidgetWindow does something similar for widgets, by keeping + // a list of popup windows, and forwarding the key event to the top-most popup.) + QWindow *focusWindow = qApp->focusWindow(); + if (focusWindow && focusWindow != q + && (focusWindow->flags() & Qt::Popup) == Qt::Popup) + QGuiApplication::sendEvent(focusWindow, e); + } +#endif } QMouseEvent *QQuickWindowPrivate::cloneMouseEvent(QMouseEvent *event, QPointF *transformedLocalPos) diff --git a/src/quick/items/qquickwindow_p.h b/src/quick/items/qquickwindow_p.h index 418633b6ac..d5c7b5d64c 100644 --- a/src/quick/items/qquickwindow_p.h +++ b/src/quick/items/qquickwindow_p.h @@ -111,6 +111,8 @@ public: QQuickItem *activeFocusItem; + void deliverKeyEvent(QKeyEvent *e); + // Keeps track of the item currently receiving mouse events QQuickItem *mouseGrabberItem; #ifndef QT_NO_CURSOR -- cgit v1.2.3