KGlobalAccel Class
Configurable global shortcut support. More...
Header: | #include <KGlobalAccel> |
CMake: | find_package(KF6 REQUIRED COMPONENTS GlobalAccel) target_link_libraries(mytarget PRIVATE KF6::GlobalAccel) |
Inherits: | QObject |
Public Types
enum | GlobalShortcutLoading { Autoloading, NoAutoloading } |
(since 5.90) enum | MatchType { Equal, Shadows, Shadowed } |
enum | actionIdFields { ComponentUnique, ActionUnique, ComponentFriendly, ActionFriendly } |
Public Functions
(since 5.0) QList<QKeySequence> | defaultShortcut(const QAction *action) const |
(since 5.10) QList<QKeySequence> | globalShortcut(const QString &componentName, const QString &actionId) const |
(since 5.0) bool | hasShortcut(const QAction *action) const |
(since 5.0) void | removeAllShortcuts(QAction *action) |
(since 5.0) bool | setDefaultShortcut(QAction *action, const QList<QKeySequence> &shortcut, KGlobalAccel::GlobalShortcutLoading loadFlag = Autoloading) |
(since 5.0) bool | setShortcut(QAction *action, const QList<QKeySequence> &shortcut, KGlobalAccel::GlobalShortcutLoading loadFlag = Autoloading) |
(since 5.0) QList<QKeySequence> | shortcut(const QAction *action) const |
Signals
(since 5.94) void | globalShortcutActiveChanged(QAction *action, bool active) |
(since 5.0) void | globalShortcutChanged(QAction *action, const QKeySequence &seq) |
Static Public Members
bool | cleanComponent(const QString &componentUnique) |
(since 5.90) QList<KGlobalShortcutInfo> | globalShortcutsByKey(const QKeySequence &seq, KGlobalAccel::MatchType type = Equal) |
bool | isComponentActive(const QString &componentName) |
(since 4.2) bool | isGlobalShortcutAvailable(const QKeySequence &seq, const QString &component = QString()) |
(since 4.2) bool | promptStealShortcutSystemwide(QWidget *parent, const QList<KGlobalShortcutInfo> &shortcuts, const QKeySequence &seq) |
KGlobalAccel * | self() |
(since 5.0) bool | setGlobalShortcut(QAction *action, const QKeySequence &shortcut) |
(since 5.0) bool | setGlobalShortcut(QAction *action, const QList<QKeySequence> &shortcuts) |
void | stealShortcutSystemwide(const QKeySequence &seq) |
Detailed Description
KGlobalAccel allows you to have global accelerators that are independent of the focused window. Unlike regular shortcuts, the application's window does not need focus for them to be activated.
Here's an example how to register the Meta + E global shortcut:
QAction *action = new QAction(); action->setObjectName(QStringLiteral("actionId")); action->setText(QStringLiteral("human readable name")); KGlobalAccel::self()->setDefaultShortcut(action, QList<QKeySequence>() << (Qt::META | Qt::Key_E)); KGlobalAccel::self()->setShortcut(action, QList<QKeySequence>() << (Qt::META | Qt::Key_E)); connect(action, &QAction::triggered, []() { qDebug() << "global shortcut trigerred"; });
See also KKeySequenceRecorder.
Member Type Documentation
enum KGlobalAccel::GlobalShortcutLoading
An enum about global shortcut setter semantics
Constant | Value | Description |
---|---|---|
KGlobalAccel::Autoloading | 0x0 | Look up the action in global settings (using its main component's name and text()) and set the shortcut as saved there. |
KGlobalAccel::NoAutoloading | 0x4 | Prevent autoloading of saved global shortcut for action. |
See also setGlobalShortcut().
[since 5.90]
enum KGlobalAccel::MatchType
Keysequence match semantics.
Assuming we have an Emacs-style shortcut, for example (Alt+B, Alt+F, Alt+G) already assigned, how a new shortcut is compared depends on which value of the enum is used.
Constant | Value | Description |
---|---|---|
KGlobalAccel::Equal | 0 | Exact matching: (Alt+B, Alt+F, Alt+G) |
KGlobalAccel::Shadows | 1 | Sequence shadowing: (Alt+B, Alt+F), (Alt+F, Alt+G) |
KGlobalAccel::Shadowed | 2 | Sequence being shadowed: (Alt+B, Alt+F, Alt+G, <any key>), (<any key>, Alt+B, Alt+F, Alt+G) |
This enum was introduced in 5.90.
enum KGlobalAccel::actionIdFields
Index for actionId QStringLists
Constant | Value | Description |
---|---|---|
KGlobalAccel::ComponentUnique | 0 | Components Unique Name (ID). |
KGlobalAccel::ActionUnique | 1 | Actions Unique Name (ID). |
KGlobalAccel::ComponentFriendly | 2 | Components Friendly Translated Name |
KGlobalAccel::ActionFriendly | 3 | Actions Friendly Translated Name |
Member Function Documentation
[static]
bool KGlobalAccel::cleanComponent(const QString &componentUnique)
Clean the shortcuts for component componentUnique.
If the component is not active all global shortcut registrations are purged and the component is removed completely.
If the component is active all global shortcut registrations not in use will be purged. If there is no shortcut registration left the component is purged too.
If a purged component or shortcut is activated the next time it will reregister itself. All you probably will lose on wrong usage are the user's set shortcuts.
If you make sure your component is running and all global shortcuts it has are active this function can be used to clean up the registry.
Handle with care!
If the method return true
at least one shortcut was purged so handle all previously acquired information with care.
[since 5.0]
QList<QKeySequence> KGlobalAccel::defaultShortcut(const QAction *action) const
Get the global default shortcut for this action, if one exists. Global shortcuts allow your actions to respond to accellerators independently of the focused window. Unlike regular shortcuts, the application's window does not need focus for them to be activated.
This function was introduced in 5.0.
See also setDefaultShortcut().
[since 5.10]
QList<QKeySequence> KGlobalAccel::globalShortcut(const QString &componentName, const QString &actionId) const
Retrieves the shortcut as defined in global settings by componentName (e.g. "kwin") and actionId (e.g. "Kill Window").
This function was introduced in 5.10.
See also setGlobalShortcut().
[signal, since 5.94]
void KGlobalAccel::globalShortcutActiveChanged(QAction *action, bool active)
Emitted when a global shortcut for the given action is activated or deactivated.
The global shorcut will be activated when the keys are pressed and deactivated when the keys are released. active indicates whether the global shortcut is active.
This function was introduced in 5.94.
[signal, since 5.0]
void KGlobalAccel::globalShortcutChanged(QAction *action, const QKeySequence &seq)
Emitted when the global shortcut is changed. A global shortcut is subject to be changed by the global shortcuts kcm.
action specifies the action for which the changed shortcut was registered.
seq indicates the key sequence that corresponds to the changed shortcut.
This function was introduced in 5.0.
See also setGlobalShortcut() and setDefaultShortcut().
[static, since 5.90]
QList<KGlobalShortcutInfo> KGlobalAccel::globalShortcutsByKey(const QKeySequence &seq, KGlobalAccel::MatchType type = Equal)
Returns a list of global shortcuts registered for the shortcut seq using the given matching type.
If the list contains more that one entry it means the component that registered the shortcuts uses global shortcut contexts. All returned shortcuts belong to the same component.
This function was introduced in 5.90.
[since 5.0]
bool KGlobalAccel::hasShortcut(const QAction *action) const
Returns true if a shortcut or a default shortcut has been registered for the given action.
This function was introduced in 5.0.
[static]
bool KGlobalAccel::isComponentActive(const QString &componentName)
Returns true
if the component with the given componentName exists; otherwise returns false
.
[static, since 4.2]
bool KGlobalAccel::isGlobalShortcutAvailable(const QKeySequence &seq, const QString &component = QString())
Returns true
if the shortcut seq is available for the component; otherwise returns false
.
The component is only of interest if the current application uses global shortcut contexts. In that case a global shortcut by component in an inactive global shortcut contexts does not block the seq for us.
This function was introduced in 4.2.
[static, since 4.2]
bool KGlobalAccel::promptStealShortcutSystemwide(QWidget *parent, const QList<KGlobalShortcutInfo> &shortcuts, const QKeySequence &seq)
Show a messagebox to inform the user that a global shortcut is already occupied, and ask to take it away from its current action(s). This is GUI only, so nothing will be actually changed.
Returns true
if user confirms that it is okay to re-assign the global shorcut; otherwise returns false
.
This function was introduced in 4.2.
See also stealShortcutSystemwide().
[since 5.0]
void KGlobalAccel::removeAllShortcuts(QAction *action)
Unregister and remove all defined global shortcuts for the given action.
This function was introduced in 5.0.
[static]
KGlobalAccel *KGlobalAccel::self()
Returns (and creates if necessary) the singleton instance
[since 5.0]
bool KGlobalAccel::setDefaultShortcut(QAction *action, const QList<QKeySequence> &shortcut, KGlobalAccel::GlobalShortcutLoading loadFlag = Autoloading)
Assign a default global shortcut for a given action.
For more information about global shortcuts and loadFlag, see setShortcut().
Upon shortcut change the globalShortcutChanged() will be triggered so other applications get notified.
Returns true
if the shortcut has been set successfully; otherwise returns false
.
This function was introduced in 5.0.
See also defaultShortcut() and globalShortcutChanged().
[static, since 5.0]
bool KGlobalAccel::setGlobalShortcut(QAction *action, const QKeySequence &shortcut)
Sets both active and default shortcut for the given action.
This method is suited for the case that only one shortcut is to be configured.
If more control for loading the shortcuts is needed use the variants offering more control.
Returns true
if the shortcut has been set successfully; otherwise returns false
.
This function was introduced in 5.0.
See also globalShortcut(), setShortcut(), and setDefaultShortcut().
[static, since 5.0]
bool KGlobalAccel::setGlobalShortcut(QAction *action, const QList<QKeySequence> &shortcuts)
Sets both active and default shortcuts for the given action.
If more control for loading the shortcuts is needed use the variants offering more control.
Returns true
if the shortcut has been set successfully; otherwise returns false
.
This function was introduced in 5.0.
See also setShortcut() and setDefaultShortcut().
[since 5.0]
bool KGlobalAccel::setShortcut(QAction *action, const QList<QKeySequence> &shortcut, KGlobalAccel::GlobalShortcutLoading loadFlag = Autoloading)
Assign a global shortcut for the given action.
Global shortcuts allow an action to respond to key shortcuts independently of the focused window, i.e. the action will trigger if the keys were pressed no matter where in the X session.
The action must have a per main component unique action->objectName() to enable cross-application bookkeeping. If the action->objectName() is empty this method will do nothing and will return false.
It is mandatory that the action->objectName() doesn't change once the shortcut has been successfully registered.
Note: KActionCollection::insert(name, action) will set action's objectName to name so you often don't have to set an objectName explicitly.
When an action, identified by main component name and objectName(), is assigned a global shortcut for the first time on a KDE installation the assignment will be saved. The shortcut will then be restored every time setGlobalShortcut() is called with loadFlag == Autoloading.
If you actually want to change the global shortcut you have to set loadFlag to NoAutoloading. The new shortcut will be automatically saved again.
action specifies the action for which the shortcut will be assigned.
shortcut specifies the global shortcut(s) to assign. Will be ignored unless loadFlag is set to NoAutoloading or this is the first time ever you call this method (see above).
If loadFlag is KGlobalAccel::Autoloading, assign the global shortcut this action has previously had if any. That way user preferences and changes made to avoid clashes will be conserved. If KGlobalAccel::NoAutoloading the given shortcut will be assigned without looking up old values. You should only do this if the user wants to change the shortcut or if you have another very good reason. Key combinations that clash with other shortcuts will be dropped.
Note: the default shortcut will never be influenced by autoloading - it will be set as given.
This function was introduced in 5.0.
See also shortcut() and globalShortcutChanged().
[since 5.0]
QList<QKeySequence> KGlobalAccel::shortcut(const QAction *action) const
Get the global shortcut for this action, if one exists. Global shortcuts allow your actions to respond to accellerators independently of the focused window. Unlike regular shortcuts, the application's window does not need focus for them to be activated.
Note: that this method only works together with setShortcut() because the action pointer is used to retrieve the result. If you would like to retrieve the shortcut as stored in the global settings, use the globalShortcut(componentName, actionId) instead.
This function was introduced in 5.0.
See also setShortcut().
[static]
void KGlobalAccel::stealShortcutSystemwide(const QKeySequence &seq)
Take away the given shortcut seq from the named action it belongs to. This applies to all actions with global shortcuts in any KDE application.
See also promptStealShortcutSystemwide().