/* * Copyright (C) 2014 Canonical, Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Author: Daniel d'Andrada */ #include "windowkeysfilter.h" #include WindowKeysFilter::WindowKeysFilter(QQuickItem *parent) : QQuickItem(parent) { connect(this, &QQuickItem::windowChanged, this, &WindowKeysFilter::setupFilterOnWindow); } bool WindowKeysFilter::eventFilter(QObject *watched, QEvent *event) { Q_ASSERT(!m_filteredWindow.isNull()); Q_ASSERT(watched == static_cast(m_filteredWindow.data())); if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) { // Let QML see this event and decide if it does not want it event->accept(); QCoreApplication::sendEvent(this, event); return event->isAccepted(); } else { // Not interested return false; } } void WindowKeysFilter::setupFilterOnWindow(QQuickWindow *window) { if (!m_filteredWindow.isNull()) { m_filteredWindow->removeEventFilter(this); m_filteredWindow.clear(); } if (window) { window->installEventFilter(this); m_filteredWindow = window; } }