18 #include <QGuiApplication> 20 #include <QKeySequence> 22 #include "globalshortcutregistry.h" 28 if (
auto item = qobject_cast<QQuickItem*>(parent)) {
29 auto window = item->window();
30 if (window)
return window;
32 parent = parent->parent();
38 GlobalShortcutRegistry::GlobalShortcutRegistry(QObject *parent)
42 setupFilterOnWindow(qGuiApp->focusWindow());
52 return m_shortcuts.contains(seq);
58 if (!m_shortcuts.contains(seq)) {
59 m_shortcuts.insert(seq, {sc});
61 auto shortcuts = m_shortcuts[seq];
63 m_shortcuts.insert(seq, shortcuts);
66 connect(sc, &GlobalShortcut::destroyed,
this, &GlobalShortcutRegistry::removeShortcut);
70 void GlobalShortcutRegistry::removeShortcut(QObject *obj)
72 QMutableMapIterator<QVariant, QVector<QPointer<GlobalShortcut>>> it(m_shortcuts);
73 while (it.hasNext()) {
76 if (scObj && it.value().contains(scObj)) {
77 it.value().removeAll(scObj);
78 if (it.value().isEmpty()) {
85 bool GlobalShortcutRegistry::eventFilter(QObject *obj, QEvent *event)
87 Q_ASSERT(m_filteredWindow);
88 Q_ASSERT(obj == static_cast<QObject*>(m_filteredWindow.data()));
90 if (event->type() == QEvent::KeyPress ||
event->type() == QEvent::KeyRelease) {
92 QKeyEvent *keyEvent =
static_cast<QKeyEvent*
>(event);
95 QKeyEvent eCopy(keyEvent->type(),
97 keyEvent->modifiers(),
99 keyEvent->isAutoRepeat(),
103 int seq = keyEvent->key() + keyEvent->modifiers();
104 if (m_shortcuts.contains(seq)) {
105 const auto shortcuts = m_shortcuts.value(seq);
106 Q_FOREACH(
const auto &shortcut, shortcuts) {
108 auto window = windowForShortcut(shortcut);
109 if (!window || window == obj) {
110 qApp->sendEvent(shortcut, &eCopy);
116 return eCopy.isAccepted();
119 return QObject::eventFilter(obj, event);
124 if (m_filteredWindow) {
125 m_filteredWindow->removeEventFilter(
this);
126 m_filteredWindow.clear();
130 m_filteredWindow = window;
131 window->installEventFilter(
this);
void setupFilterOnWindow(QWindow *window)
void addShortcut(const QVariant &seq, GlobalShortcut *sc)
The GlobalShortcut class.
GlobalShortcutList shortcuts() const
bool hasShortcut(const QVariant &seq) const