File indexing completed on 2024-11-24 04:54:31

0001 /*
0002     SPDX-FileCopyrightText: 2022 Aditya Mehra <aix.m@outlook.com>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "keyfilter.h"
0007 #include <QKeyEvent>
0008 #include <QDebug>
0009 
0010 KeyFilter::KeyFilter(QObject *parent) : QObject(parent)
0011 {
0012 }
0013 
0014 void KeyFilter::startFilter()
0015 {
0016     if (m_filtering) {
0017         return;
0018     }
0019 
0020     m_filtering = true;
0021     Q_EMIT filterStarted();
0022 }
0023 
0024 void KeyFilter::stopFilter()
0025 {
0026     if (!m_filtering) {
0027         return;
0028     }
0029 
0030     m_filtering = false;
0031     Q_EMIT filterStopped();
0032 }
0033 
0034 bool KeyFilter::isFiltering()
0035 {
0036     return m_filtering;
0037 }
0038 
0039 bool KeyFilter::eventFilter(QObject *obj, QEvent *event)
0040 {
0041     if (!m_filtering) {
0042         return false;
0043     }
0044 
0045     if (event->type() == QEvent::KeyPress) {
0046         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
0047         if (keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right || keyEvent->key() == Qt::Key_Up || keyEvent->key() == Qt::Key_Down || keyEvent->key() == Qt::Key_Return){
0048             Q_EMIT keyPress(event);
0049         }
0050 
0051     } else if (event->type() == QEvent::KeyRelease) {
0052         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
0053         if (keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right || keyEvent->key() == Qt::Key_Up || keyEvent->key() == Qt::Key_Down || keyEvent->key() == Qt::Key_Return) {
0054             Q_EMIT keyRelease(event);
0055         }
0056     }
0057 
0058     return QObject::eventFilter(obj, event);
0059 }