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();
38GlobalShortcutRegistry::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});
66 connect(sc, &GlobalShortcut::destroyed,
this, &GlobalShortcutRegistry::removeShortcut);
70void 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()) {
85bool 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(),
102 int seq = keyEvent->key() + keyEvent->modifiers();
103 bool acceptedAtLeastOnce =
false;
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);
111 acceptedAtLeastOnce = acceptedAtLeastOnce || eCopy.isAccepted();
117 return acceptedAtLeastOnce;
120 return QObject::eventFilter(obj, event);
125 if (m_filteredWindow) {
126 m_filteredWindow->removeEventFilter(
this);
127 m_filteredWindow.clear();
131 m_filteredWindow = window;
132 window->installEventFilter(
this);
The GlobalShortcut class.
bool hasShortcut(const QVariant &seq) const
void setupFilterOnWindow(QWindow *window)
void addShortcut(const QVariant &seq, GlobalShortcut *sc)
GlobalShortcutList shortcuts() const