From b9a7c773f1b20a86ea11fe63ee57eb5713df9041 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Tue, 25 Oct 2016 19:16:10 +0200 Subject: [PATCH] WebView: Fix handling of key events Unhandled key events are sent to parent of QWebEngineView. Hack with event filter on RenderWidgetHostViewQtDelegateWidget no longer works for key events, so we need to install event filter on view parent and handle key events there. Downside is, that we can now only handle events that are not handled by page itself. --- src/lib/webengine/webview.cpp | 64 ++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/src/lib/webengine/webview.cpp b/src/lib/webengine/webview.cpp index 124ced478..af2ba311e 100644 --- a/src/lib/webengine/webview.cpp +++ b/src/lib/webengine/webview.cpp @@ -1079,13 +1079,6 @@ void WebView::_keyPressEvent(QKeyEvent *event) } break; - case Qt::Key_Escape: - if (isFullScreen()) { - triggerPageAction(QWebEnginePage::ExitFullScreen); - event->accept(); - } - break; - default: break; } @@ -1096,6 +1089,18 @@ void WebView::_keyReleaseEvent(QKeyEvent *event) if (mApp->plugins()->processKeyRelease(Qz::ON_WebView, this, event)) { event->accept(); } + + switch (event->key()) { + case Qt::Key_Escape: + if (isFullScreen()) { + triggerPageAction(QWebEnginePage::ExitFullScreen); + event->accept(); + } + break; + + default: + break; + } } void WebView::_contextMenuEvent(QContextMenuEvent *event) @@ -1143,25 +1148,24 @@ bool WebView::eventFilter(QObject *obj, QEvent *event) } } + // Keyboard events are sent to parent widget + if (obj == this && event->type() == QEvent::ParentChange && parent()) { + parent()->installEventFilter(this); + } + // Forward events to WebView - if (obj == m_rwhvqt) { #define HANDLE_EVENT(f, t) \ - { \ - bool wasAccepted = event->isAccepted(); \ - event->setAccepted(false); \ - f(static_cast(event)); \ - bool ret = event->isAccepted(); \ - event->setAccepted(wasAccepted); \ - return ret; \ - } + { \ + bool wasAccepted = event->isAccepted(); \ + event->setAccepted(false); \ + f(static_cast(event)); \ + bool ret = event->isAccepted(); \ + event->setAccepted(wasAccepted); \ + return ret; \ + } + if (obj == m_rwhvqt) { switch (event->type()) { - case QEvent::KeyPress: - HANDLE_EVENT(_keyPressEvent, QKeyEvent); - - case QEvent::KeyRelease: - HANDLE_EVENT(_keyReleaseEvent, QKeyEvent); - case QEvent::MouseButtonPress: HANDLE_EVENT(_mousePressEvent, QMouseEvent); @@ -1177,10 +1181,22 @@ bool WebView::eventFilter(QObject *obj, QEvent *event) default: break; } - -#undef HANDLE_EVENT } + if (obj == parentWidget()) { + switch (event->type()) { + case QEvent::KeyPress: + HANDLE_EVENT(_keyPressEvent, QKeyEvent); + + case QEvent::KeyRelease: + HANDLE_EVENT(_keyReleaseEvent, QKeyEvent); + + default: + break; + } + } +#undef HANDLE_EVENT + // Block already handled events if (obj == this) { switch (event->type()) {