on xenial help sign in
global shortcut release
commit6c3fa9679ff16e2485176429fb8ed7d4e04a156d authorNick Dedekind <nick.dedekind@canonical.com> dateWed 12 Oct 2016, 18:11 (10 years ago) parent5692bb9
5 files changed+35−32
plugins/GlobalShortcut/globalshortcut.cpp+10−4 plugins/GlobalShortcut/globalshortcut.h+5 plugins/GlobalShortcut/globalshortcutregistry.cpp+14−4 plugins/Utils/windowinputfilter.cpp+6−23 plugins/Utils/windowinputfilter.h−1
plugins/GlobalShortcut/globalshortcut.cpp+10−4
@@ -63,10 +63,16 @@ void GlobalShortcut::componentComplete()
6363
6464void GlobalShortcut::keyPressEvent(QKeyEvent * event)
6565{
66− Q_UNUSED(event)
67− if (m_active) {
68− Q_EMIT triggered(m_shortcut.toString());
69− }
66+ if (!m_active) return;
67+
68+ event->accept();
69+ Q_EMIT triggered(m_shortcut.toString());
70+}
71+
72+void GlobalShortcut::keyReleaseEvent(QKeyEvent * event)
73+{
74+ event->accept();
75+ Q_EMIT released(m_shortcut.toString());
7076}
7177
7278void GlobalShortcut::setupFilterOnWindow(QQuickWindow *window)
plugins/GlobalShortcut/globalshortcut.h+5
@@ -55,6 +55,7 @@ public:
5555protected:
5656 void componentComplete() override;
5757 void keyPressEvent(QKeyEvent * event) override;
58+ void keyReleaseEvent(QKeyEvent * event) override;
5859
5960Q_SIGNALS:
6061 void shortcutChanged(const QVariant &shortcut);
@@ -62,6 +63,10 @@ Q_SIGNALS:
6263 * Emitted when a global keypress of @p shortcut is detected
6364 */
6465 void triggered(const QString &shortcut);
66+ /**
67+ * Emitted when a global keypress of @p shortcut is released
68+ */
69+ void released(const QString &shortcut);
6570 void activeChanged(bool active);
6671
6772private Q_SLOTS:
plugins/GlobalShortcut/globalshortcutregistry.cpp+14−4
@@ -73,20 +73,30 @@ bool GlobalShortcutRegistry::eventFilter(QObject *obj, QEvent *event)
7373 Q_ASSERT(m_filteredWindow);
7474 Q_ASSERT(obj == static_cast<QObject*>(m_filteredWindow.data()));
7575
76− if (event->type() == QEvent::KeyPress) {
76+ if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
77+
7778 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
79+
80+ // Make a copy of the event so we don't alter it for passing on.
81+ QKeyEvent eCopy(keyEvent->type(),
82+ keyEvent->key(),
83+ keyEvent->modifiers(),
84+ keyEvent->text(),
85+ keyEvent->isAutoRepeat(),
86+ keyEvent->count());
87+ eCopy.ignore();
88+
7889 int seq = keyEvent->key() + keyEvent->modifiers();
7990 if (m_shortcuts.contains(seq)) {
8091 const auto shortcuts = m_shortcuts.value(seq);
8192 Q_FOREACH(const auto &shortcut, shortcuts) {
8293 if (shortcut) {
83− qApp->sendEvent(shortcut, keyEvent);
84− event->accept();
94+ qApp->sendEvent(shortcut, &eCopy);
8595 }
8696 }
8797 }
8898
89− return event->isAccepted();
99+ return eCopy.isAccepted();
90100 }
91101
92102 return QObject::eventFilter(obj, event);
plugins/Utils/windowinputfilter.cpp+6−23
@@ -26,14 +26,10 @@ WindowInputFilter::WindowInputFilter(QQuickItem *parent)
2626{
2727 connect(this, &QQuickItem::windowChanged,
2828 this, &WindowInputFilter::setupFilterOnWindow);
29− connect(this, &QQuickItem::enabledChanged,
30− this, &WindowInputFilter::updateFilter);
3129}
3230
3331bool WindowInputFilter::eventFilter(QObject *watched, QEvent *event)
3432{
35− if (!isEnabled()) return false;
36−
3733 Q_ASSERT(!m_filteredWindow.isNull());
3834 Q_ASSERT(watched == static_cast<QObject*>(m_filteredWindow.data()));
3935 Q_UNUSED(watched);
@@ -57,27 +53,14 @@ bool WindowInputFilter::eventFilter(QObject *watched, QEvent *event)
5753
5854void WindowInputFilter::setupFilterOnWindow(QQuickWindow *window)
5955{
60− if (isEnabled()) {
61− if (!m_filteredWindow.isNull()) {
62− m_filteredWindow->removeEventFilter(this);
63− m_filteredWindow.clear();
64− }
65−
66− if (window) {
67− window->installEventFilter(this);
68− }
56+ if (!m_filteredWindow.isNull()) {
57+ m_filteredWindow->removeEventFilter(this);
58+ m_filteredWindow.clear();
6959 }
70− m_filteredWindow = window;
71−}
7260
73−void WindowInputFilter::updateFilter()
74−{
75− if (!m_filteredWindow.isNull()) {
76− if (isEnabled()) {
77− m_filteredWindow->installEventFilter(this);
78− } else {
79− m_filteredWindow->removeEventFilter(this);
80− }
61+ if (window) {
62+ window->installEventFilter(this);
63+ m_filteredWindow = window;
8164 }
8265}
8366
plugins/Utils/windowinputfilter.h−1
@@ -49,7 +49,6 @@ Q_SIGNALS:
4949
5050private Q_SLOTS:
5151 void setupFilterOnWindow(QQuickWindow *window);
52− void updateFilter();
5352
5453private:
5554 QPointer<QQuickWindow> m_filteredWindow;