Loading...
Searching...
No Matches
GlobalShortcutRegistry Class Reference

The GlobalShortcutRegistry class. More...

`#include <plugins/GlobalShortcut/globalshortcutregistry.h>`

## Public Member Functions

| GlobalShortcutRegistry (QObject *parent=nullptr)
GlobalShortcutList | shortcuts () const
bool | hasShortcut (const QVariant &seq) const
void | addShortcut (const QVariant &seq, GlobalShortcut *sc)
void | setupFilterOnWindow (QWindow *window)

## Protected Member Functions

bool | eventFilter (QObject *obj, QEvent *event) override

## Detailed Description

The GlobalShortcutRegistry class.

Serves as a central point for shortcut registration.

Definition at line 34 of file globalshortcutregistry.h.

## Constructor & Destructor Documentation

## GlobalShortcutRegistry()

GlobalShortcutRegistry::GlobalShortcutRegistry | ( | QObject * | parent = nullptr | ) |

Definition at line 38 of file globalshortcutregistry.cpp.

: QObject(parent)
{
connect(qGuiApp, &QGuiApplication::focusWindowChanged, this, &GlobalShortcutRegistry::setupFilterOnWindow);
setupFilterOnWindow(qGuiApp->focusWindow());
}
GlobalShortcutRegistry::setupFilterOnWindow
void setupFilterOnWindow(QWindow *window)
Definition globalshortcutregistry.cpp:123

## Member Function Documentation

## addShortcut()

void GlobalShortcutRegistry::addShortcut | ( | const QVariant & | seq,
|  | GlobalShortcut * | sc )

Adds a shortcut `seq` to the registry

Definition at line 55 of file globalshortcutregistry.cpp.

{
if (sc) {
if (!m_shortcuts.contains(seq)) { // create a new entry
m_shortcuts.insert(seq, {sc});
} else { // append to an existing one
auto shortcuts = m_shortcuts[seq];
shortcuts.append(sc);
m_shortcuts.insert(seq, shortcuts);
}
connect(sc, &GlobalShortcut::destroyed, this, &GlobalShortcutRegistry::removeShortcut);
}
}
GlobalShortcutRegistry::shortcuts
GlobalShortcutList shortcuts() const
Definition globalshortcutregistry.cpp:45

## eventFilter()

bool GlobalShortcutRegistry::eventFilter | ( | QObject * | obj,
|  | QEvent * | event )

override protected

Definition at line 85 of file globalshortcutregistry.cpp.

{
Q_ASSERT(m_filteredWindow);
Q_ASSERT(obj == static_cast<QObject*>(m_filteredWindow.data()));
if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
// Make a copy of the event so we don't alter it for passing on.
QKeyEvent eCopy(keyEvent->type(),
keyEvent->key(),
keyEvent->modifiers(),
keyEvent->text(),
keyEvent->isAutoRepeat(),
keyEvent->count());
int seq = keyEvent->key() + keyEvent->modifiers();
bool acceptedAtLeastOnce = false;
if (m_shortcuts.contains(seq)) {
const auto shortcuts = m_shortcuts.value(seq);
Q_FOREACH(const auto &shortcut, shortcuts) {
if (shortcut) {
auto window = windowForShortcut(shortcut);
if (!window || window == obj) { // accept shortcut if it's not attached to a window or it's window is active.
qApp->sendEvent(shortcut, &eCopy);
acceptedAtLeastOnce = acceptedAtLeastOnce || eCopy.isAccepted();
}
}
}
}
return acceptedAtLeastOnce;
}
return QObject::eventFilter(obj, event);
}

## hasShortcut()

bool GlobalShortcutRegistry::hasShortcut | ( | const QVariant & | seq | ) | const

Returns
  whether shortcut `seq` is currently registered

Definition at line 50 of file globalshortcutregistry.cpp.

{
return m_shortcuts.contains(seq);
}

## setupFilterOnWindow()

void GlobalShortcutRegistry::setupFilterOnWindow | ( | QWindow * | window | ) |

Sets up key events filtering on window `window`

Definition at line 123 of file globalshortcutregistry.cpp.

{
if (m_filteredWindow) {
m_filteredWindow->removeEventFilter(this);
m_filteredWindow.clear();
}
if (window) {
m_filteredWindow = window;
window->installEventFilter(this);
}
}

## shortcuts()

GlobalShortcutList GlobalShortcutRegistry::shortcuts | ( |  | ) | const

Returns
  the list of shortcuts currently registered

Definition at line 45 of file globalshortcutregistry.cpp.

{
return m_shortcuts;
}

The documentation for this class was generated from the following files:

- plugins/GlobalShortcut/globalshortcutregistry.h
- plugins/GlobalShortcut/globalshortcutregistry.cpp