on xenial help sign in
focus window gets global shortcuts
15874ddNick Dedekind
plugins/GlobalShortcut/globalshortcut.cpp−15
@@ -56,11 +56,6 @@ void GlobalShortcut::setActive(bool active)
56 Q_EMIT activeChanged(active);
56 Q_EMIT activeChanged(active);
57}
57}
58
58
59−void GlobalShortcut::componentComplete()
60−{
61− connect(this, &QQuickItem::windowChanged, this, &GlobalShortcut::setupFilterOnWindow);
62−}
63−
64void GlobalShortcut::keyPressEvent(QKeyEvent * event)
59void GlobalShortcut::keyPressEvent(QKeyEvent * event)
65{
60{
66 if (!m_active) return;
61 if (!m_active) return;
@@ -76,13 +71,3 @@ void GlobalShortcut::keyReleaseEvent(QKeyEvent * event)
76 event->accept();
71 event->accept();
77 Q_EMIT released(m_shortcut.toString());
72 Q_EMIT released(m_shortcut.toString());
78}
73}
79−
80−void GlobalShortcut::setupFilterOnWindow(QQuickWindow *window)
81−{
82− if (!window) {
83−// qWarning() << Q_FUNC_INFO << "Failed to setup filter on window";
84− return;
85− }
86−
87− registry->setupFilterOnWindow((qulonglong) window->winId());
88−}
plugins/GlobalShortcut/globalshortcut.h−4
@@ -53,7 +53,6 @@ public:
53 void setActive(bool active);
53 void setActive(bool active);
54
54
55protected:
55protected:
56− void componentComplete() override;
57 void keyPressEvent(QKeyEvent * event) override;
56 void keyPressEvent(QKeyEvent * event) override;
58 void keyReleaseEvent(QKeyEvent * event) override;
57 void keyReleaseEvent(QKeyEvent * event) override;
59
58
@@ -69,9 +68,6 @@ Q_SIGNALS:
69 void released(const QString &shortcut);
68 void released(const QString &shortcut);
70 void activeChanged(bool active);
69 void activeChanged(bool active);
71
70
72−private Q_SLOTS:
73− void setupFilterOnWindow(QQuickWindow* window);
74−
75private:
71private:
76 QVariant m_shortcut;
72 QVariant m_shortcut;
77 bool m_active = true;
73 bool m_active = true;
plugins/GlobalShortcut/globalshortcutregistry.cpp+7−16
@@ -21,11 +21,11 @@
21
21
22#include "globalshortcutregistry.h"
22#include "globalshortcutregistry.h"
23
23
24−static qulonglong s_windowId = 0;
25−
26GlobalShortcutRegistry::GlobalShortcutRegistry(QObject *parent)
24GlobalShortcutRegistry::GlobalShortcutRegistry(QObject *parent)
27 : QObject(parent)
25 : QObject(parent)
28{
26{
27+ connect(qGuiApp, &QGuiApplication::focusWindowChanged, this, &GlobalShortcutRegistry::setupFilterOnWindow);
28+ setupFilterOnWindow(qGuiApp->focusWindow());
29}
29}
30
30
31GlobalShortcutList GlobalShortcutRegistry::shortcuts() const
31GlobalShortcutList GlobalShortcutRegistry::shortcuts() const
@@ -90,7 +90,7 @@ bool GlobalShortcutRegistry::eventFilter(QObject *obj, QEvent *event)
90 if (m_shortcuts.contains(seq)) {
90 if (m_shortcuts.contains(seq)) {
91 const auto shortcuts = m_shortcuts.value(seq);
91 const auto shortcuts = m_shortcuts.value(seq);
92 Q_FOREACH(const auto &shortcut, shortcuts) {
92 Q_FOREACH(const auto &shortcut, shortcuts) {
93− if (shortcut) {
93+ if (shortcut && shortcut->window() == obj) {
94 qApp->sendEvent(shortcut, &eCopy);
94 qApp->sendEvent(shortcut, &eCopy);
95 }
95 }
96 }
96 }
@@ -102,24 +102,15 @@ bool GlobalShortcutRegistry::eventFilter(QObject *obj, QEvent *event)
102 return QObject::eventFilter(obj, event);
102 return QObject::eventFilter(obj, event);
103}
103}
104
104
105−void GlobalShortcutRegistry::setupFilterOnWindow(qulonglong wid)
105+void GlobalShortcutRegistry::setupFilterOnWindow(QWindow* window)
106{
106{
107− if (wid == s_windowId) {
108− return;
109− }
110−
111 if (m_filteredWindow) {
107 if (m_filteredWindow) {
112 m_filteredWindow->removeEventFilter(this);
108 m_filteredWindow->removeEventFilter(this);
113 m_filteredWindow.clear();
109 m_filteredWindow.clear();
114− s_windowId = 0;
115 }
110 }
116
111
117− Q_FOREACH(QWindow *window, qApp->allWindows()) {
118− if (window && window->winId() == wid) {
112+ if (window) {
119− m_filteredWindow = window;
113+ m_filteredWindow = window;
120− window->installEventFilter(this);
114+ window->installEventFilter(this);
121− s_windowId = wid;
122− break;
123− }
124 }
115 }
125}
116}
plugins/GlobalShortcut/globalshortcutregistry.h+2−2
@@ -52,9 +52,9 @@ public:
52 void addShortcut(const QVariant &seq, GlobalShortcut * sc);
52 void addShortcut(const QVariant &seq, GlobalShortcut * sc);
53
53
54 /**
54 /**
55− * Sets up key events filtering on window @p wid
55+ * Sets up key events filtering on window @p window
56 */
56 */
57− void setupFilterOnWindow(qulonglong wid);
57+ void setupFilterOnWindow(QWindow* window);
58
58
59protected:
59protected:
60 bool eventFilter(QObject *obj, QEvent *event) override;
60 bool eventFilter(QObject *obj, QEvent *event) override;