SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
commandkeyreceiver.cpp
Go to the documentation of this file.
1 // Copyright (C) 2018 Edouard Griffiths, F4EXB //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #include <gui/commandkeyreceiver.h>
19 #include <QEvent>
20 #include <QKeyEvent>
21 
22 const std::vector<Qt::Key> CommandKeyReceiver::m_composeKeys = {Qt::Key_Shift, Qt::Key_Control, Qt::Key_Meta, Qt::Key_Alt, Qt::Key_AltGr};
23 
25  m_release(false),
26  m_pass(true)
27 {
28 }
29 
30 bool CommandKeyReceiver::eventFilter(QObject* obj, QEvent* event)
31 {
32  if (event->type() == QEvent::KeyPress)
33  {
34  QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
35 
36  if ((!keyEvent->isAutoRepeat()) && (!isComposeKey(static_cast<Qt::Key>(keyEvent->key()))))
37  {
38 // qDebug("KeyReceiver::eventFilter: KeyPress");
39  Qt::Key key;
40  Qt::KeyboardModifiers keyModifiers;
41  keyEventHandler(keyEvent, key, keyModifiers);
42  emit capturedKey(key, keyModifiers, false);
43 
44  if (!m_pass) { // do not pass the event
45  return true;
46  }
47  }
48  }
49  else if (m_release && (event->type()==QEvent::KeyRelease))
50  {
51  QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
52 
53  if ((!keyEvent->isAutoRepeat()) && (!isComposeKey(static_cast<Qt::Key>(keyEvent->key()))))
54  {
55 // qDebug("KeyReceiver::eventFilter: KeyRelease");
56  Qt::Key key;
57  Qt::KeyboardModifiers keyModifiers;
58  keyEventHandler(keyEvent, key, keyModifiers);
59  emit capturedKey(key, keyModifiers, true);
60 
61  if (!m_pass) { // do not pass the event
62  return true;
63  }
64  }
65  }
66 
67  return QObject::eventFilter(obj, event); // pass the event on
68 }
69 
70 void CommandKeyReceiver::keyEventHandler(QKeyEvent *e, Qt::Key& key, Qt::KeyboardModifiers& keyModifiers)
71 {
72  key = static_cast<Qt::Key>(e->key());
73 
74  if (e->modifiers())
75  {
76  keyModifiers = e->modifiers();
77  }
78  else
79  {
80  keyModifiers = Qt::NoModifier;
81  }
82 }
83 
85 {
86  auto it = std::find(m_composeKeys.begin(), m_composeKeys.end(), key);
87  return it != m_composeKeys.end();
88 }
bool m_release
check release events
void keyEventHandler(QKeyEvent *e, Qt::Key &key, Qt::KeyboardModifiers &keyModifiers)
bool m_pass
do not block events just tap them
static const std::vector< Qt::Key > m_composeKeys
void capturedKey(Qt::Key key, Qt::KeyboardModifiers keyModifiers, bool release)
bool isComposeKey(Qt::Key key)
bool eventFilter(QObject *obj, QEvent *event)