2018-01-04 23:05:12 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
2023-11-19 00:43:20 -05:00
|
|
|
// Copyright (C) 2018-2019, 2022 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
|
2018-01-04 23:05:12 -05:00
|
|
|
// //
|
|
|
|
// 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 as version 3 of the License, or //
|
2019-04-11 08:43:33 -04:00
|
|
|
// (at your option) any later version. //
|
2018-01-04 23:05:12 -05:00
|
|
|
// //
|
|
|
|
// 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 V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QKeyEvent>
|
|
|
|
|
2022-01-06 16:47:41 -05:00
|
|
|
#include "commandkeyreceiver.h"
|
|
|
|
|
2018-01-04 23:05:12 -05:00
|
|
|
const std::vector<Qt::Key> CommandKeyReceiver::m_composeKeys = {Qt::Key_Shift, Qt::Key_Control, Qt::Key_Meta, Qt::Key_Alt, Qt::Key_AltGr};
|
|
|
|
|
|
|
|
CommandKeyReceiver::CommandKeyReceiver() :
|
|
|
|
m_release(false),
|
|
|
|
m_pass(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CommandKeyReceiver::eventFilter(QObject* obj, QEvent* event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::KeyPress)
|
|
|
|
{
|
|
|
|
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
|
|
|
|
|
|
|
|
if ((!keyEvent->isAutoRepeat()) && (!isComposeKey(static_cast<Qt::Key>(keyEvent->key()))))
|
|
|
|
{
|
|
|
|
// qDebug("KeyReceiver::eventFilter: KeyPress");
|
|
|
|
Qt::Key key;
|
|
|
|
Qt::KeyboardModifiers keyModifiers;
|
|
|
|
keyEventHandler(keyEvent, key, keyModifiers);
|
|
|
|
emit capturedKey(key, keyModifiers, false);
|
|
|
|
|
2018-01-05 03:59:37 -05:00
|
|
|
if (!m_pass) { // do not pass the event
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-04 23:05:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_release && (event->type()==QEvent::KeyRelease))
|
|
|
|
{
|
|
|
|
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
|
|
|
|
|
|
|
|
if ((!keyEvent->isAutoRepeat()) && (!isComposeKey(static_cast<Qt::Key>(keyEvent->key()))))
|
|
|
|
{
|
|
|
|
// qDebug("KeyReceiver::eventFilter: KeyRelease");
|
|
|
|
Qt::Key key;
|
|
|
|
Qt::KeyboardModifiers keyModifiers;
|
|
|
|
keyEventHandler(keyEvent, key, keyModifiers);
|
|
|
|
emit capturedKey(key, keyModifiers, true);
|
|
|
|
|
2018-01-05 03:59:37 -05:00
|
|
|
if (!m_pass) { // do not pass the event
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-04 23:05:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 03:59:37 -05:00
|
|
|
return QObject::eventFilter(obj, event); // pass the event on
|
2018-01-04 23:05:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void CommandKeyReceiver::keyEventHandler(QKeyEvent *e, Qt::Key& key, Qt::KeyboardModifiers& keyModifiers)
|
|
|
|
{
|
|
|
|
key = static_cast<Qt::Key>(e->key());
|
|
|
|
|
|
|
|
if (e->modifiers())
|
|
|
|
{
|
|
|
|
keyModifiers = e->modifiers();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
keyModifiers = Qt::NoModifier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CommandKeyReceiver::isComposeKey(Qt::Key key)
|
|
|
|
{
|
|
|
|
auto it = std::find(m_composeKeys.begin(), m_composeKeys.end(), key);
|
|
|
|
return it != m_composeKeys.end();
|
|
|
|
}
|