18 #include <QGuiApplication> 20 #include <QKeySequence> 22 #include "globalshortcutregistry.h" 24 static qulonglong s_windowId = 0;
26 GlobalShortcutRegistry::GlobalShortcutRegistry(QObject *parent)
38 return m_shortcuts.contains(seq);
44 if (!m_shortcuts.contains(seq)) {
45 m_shortcuts.insert(seq, {sc});
47 auto shortcuts = m_shortcuts[seq];
49 m_shortcuts.insert(seq, shortcuts);
52 connect(sc, &GlobalShortcut::destroyed,
this, &GlobalShortcutRegistry::removeShortcut);
56 void GlobalShortcutRegistry::removeShortcut(QObject *obj)
58 QMutableMapIterator<QVariant, QVector<QPointer<GlobalShortcut>>> it(m_shortcuts);
59 while (it.hasNext()) {
62 if (scObj && it.value().contains(scObj)) {
63 it.value().removeAll(scObj);
64 if (it.value().isEmpty()) {
71 bool GlobalShortcutRegistry::eventFilter(QObject *obj, QEvent *event)
73 Q_ASSERT(m_filteredWindow);
74 Q_ASSERT(obj == static_cast<QObject*>(m_filteredWindow.data()));
76 if (event->type() == QEvent::KeyPress ||
event->type() == QEvent::KeyRelease) {
78 QKeyEvent *keyEvent =
static_cast<QKeyEvent*
>(event);
81 QKeyEvent eCopy(keyEvent->type(),
83 keyEvent->modifiers(),
85 keyEvent->isAutoRepeat(),
89 int seq = keyEvent->key() + keyEvent->modifiers();
90 if (m_shortcuts.contains(seq)) {
91 const auto shortcuts = m_shortcuts.value(seq);
92 Q_FOREACH(
const auto &shortcut, shortcuts) {
94 qApp->sendEvent(shortcut, &eCopy);
99 return eCopy.isAccepted();
102 return QObject::eventFilter(obj, event);
107 if (wid == s_windowId) {
111 if (m_filteredWindow) {
112 m_filteredWindow->removeEventFilter(
this);
113 m_filteredWindow.clear();
117 Q_FOREACH(QWindow *window, qApp->allWindows()) {
118 if (window && window->winId() == wid) {
119 m_filteredWindow = window;
120 window->installEventFilter(
this);
void addShortcut(const QVariant &seq, GlobalShortcut *sc)
void setupFilterOnWindow(qulonglong wid)
The GlobalShortcut class.
GlobalShortcutList shortcuts() const
bool hasShortcut(const QVariant &seq) const