KKeySequenceWidget Class
A widget to input a QKeySequence. More...
Header: | #include <KKeySequenceWidget> |
CMake: | find_package(KF6 REQUIRED COMPONENTS XmlGui) target_link_libraries(mytarget PRIVATE KF6::XmlGui) |
Inherits: | QWidget |
Public Types
enum | ShortcutType { None, LocalShortcuts, StandardShortcuts, GlobalShortcuts } |
flags | ShortcutTypes |
enum | Validation { Validate, NoValidate } |
Properties
|
|
Public Functions
KKeySequenceWidget(QWidget *parent = nullptr) | |
(since 4.2) KKeySequenceWidget::ShortcutTypes | checkForConflictsAgainst() const |
(since 4.2) bool | isKeySequenceAvailable(const QKeySequence &seq) const |
bool | isModifierlessAllowed() |
(since 5.65) QKeySequence | keySequence() const |
(since 6.1) bool | modifierOnlyAllowed() const |
bool | multiKeyShortcutsAllowed() const |
(since 4.1) void | setCheckActionCollections(const QList<KActionCollection *> &actionCollections) |
(since 4.2) void | setCheckForConflictsAgainst(KKeySequenceWidget::ShortcutTypes types) |
void | setClearButtonShown(bool show) |
void | setComponentName(const QString &componentName) |
(since 6.1) void | setModifierOnlyAllowed(bool allow) |
void | setModifierlessAllowed(bool allow) |
void | setMultiKeyShortcutsAllowed(bool) |
Public Slots
void | applyStealShortcut() |
void | captureKeySequence() |
void | clearKeySequence() |
(since 5.65) void | setKeySequence(const QKeySequence &seq, KKeySequenceWidget::Validation val = NoValidate) |
Signals
(since 5.65) void | keySequenceChanged(const QKeySequence &seq) |
void | stealShortcut(const QKeySequence &seq, QAction *action) |
Detailed Description
This widget lets the user choose a QKeySequence, which is usually used as a shortcut key. The recording is initiated by calling captureKeySequence() or by the user clicking on the widget.
The widgets provides support for conflict handling.
See also setCheckForConflictsAgainst().
Member Type Documentation
enum KKeySequenceWidget::ShortcutType
flags KKeySequenceWidget::ShortcutTypes
Constant | Value | Description |
---|---|---|
KKeySequenceWidget::None | 0x00 | No checking for conflicts. |
KKeySequenceWidget::LocalShortcuts | 0x01 | Check with local shortcuts. |
KKeySequenceWidget::StandardShortcuts | 0x02 | Check against standard shortcuts. |
KKeySequenceWidget::GlobalShortcuts | 0x04 | Check against global shortcuts. |
The ShortcutTypes type is a typedef for QFlags<ShortcutType>. It stores an OR combination of ShortcutType values.
See also setCheckActionCollections(), KStandardShortcut, and KGlobalAccel.
enum KKeySequenceWidget::Validation
An enum about validation when setting a key sequence.
Constant | Value | Description |
---|---|---|
KKeySequenceWidget::Validate | 0 | Validate key sequence. |
KKeySequenceWidget::NoValidate | 1 | Use key sequence without validation. |
See also setKeySequence().
Property Documentation
checkForConflictsAgainst : ShortcutTypes
Access functions:
KKeySequenceWidget::ShortcutTypes | checkForConflictsAgainst() const |
void | setCheckForConflictsAgainst(KKeySequenceWidget::ShortcutTypes types) |
keySequence : QKeySequence
Access functions:
QKeySequence | keySequence() const |
void | setKeySequence(const QKeySequence &seq, KKeySequenceWidget::Validation val = NoValidate) |
Notifier signal:
void | keySequenceChanged(const QKeySequence &seq) |
modifierOnlyAllowed : bool
Access functions:
bool | modifierOnlyAllowed() const |
void | setModifierOnlyAllowed(bool allow) |
modifierlessAllowed : bool
Access functions:
bool | isModifierlessAllowed() |
void | setModifierlessAllowed(bool allow) |
multiKeyShortcutsAllowed : bool
Access functions:
bool | multiKeyShortcutsAllowed() const |
void | setMultiKeyShortcutsAllowed(bool) |
Member Function Documentation
[explicit]
KKeySequenceWidget::KKeySequenceWidget(QWidget *parent = nullptr)
Constructs a widget used to input a QKeySequence.
[slot]
void KKeySequenceWidget::applyStealShortcut()
Actually remove the shortcut that the user wanted to steal, from the action that was using it.
This only applies to actions provided to us by setCheckActionCollections() and setCheckActionList().
Global and Standard Shortcuts have to be stolen immediately when the user gives their consent (technical reasons). That means those changes will be active even if you never call applyStealShortcut().
To be called before you apply your changes. No local shortcuts are stolen until this function is called.
[slot]
void KKeySequenceWidget::captureKeySequence()
Capture a shortcut from the keyboard.
This call will only return once a key sequence has been captured or input was aborted.
If a key sequence was input, keySequenceChanged() will be emitted.
See also setModifierlessAllowed().
[since 4.2]
KKeySequenceWidget::ShortcutTypes KKeySequenceWidget::checkForConflictsAgainst() const
The shortcut types we check for conflicts.
Note: Getter function for property checkForConflictsAgainst.
This function was introduced in 4.2.
See also setCheckForConflictsAgainst().
[slot]
void KKeySequenceWidget::clearKeySequence()
Clears the key sequence.
[since 4.2]
bool KKeySequenceWidget::isKeySequenceAvailable(const QKeySequence &seq) const
Returns whether the key sequence seq is available to grab.
The sequence is checked under the same rules as if it has been typed by the user. This method is useful if you get key sequences from another input source and want to check if it is save to set them.
This function was introduced in 4.2.
bool KKeySequenceWidget::isModifierlessAllowed()
Returns whether "plain" keys without modifiers (like Ctrl, Alt, Meta) are allowed.
Note: Getter function for property modifierlessAllowed.
See also setModifierlessAllowed().
[since 5.65]
QKeySequence KKeySequenceWidget::keySequence() const
Returns the currently selected key sequence.
Note: Getter function for property keySequence.
This function was introduced in 5.65.
See also setKeySequence().
[signal, since 5.65]
void KKeySequenceWidget::keySequenceChanged(const QKeySequence &seq)
This signal is emitted when the current key sequence seq has changed, be it by user input or programmatically.
Note: Notifier signal for property keySequence.
This function was introduced in 5.65.
[since 6.1]
bool KKeySequenceWidget::modifierOnlyAllowed() const
Returns whether modifier-only key sequences are allowed.
Note: Getter function for property modifierOnlyAllowed.
This function was introduced in 6.1.
See also setModifierOnlyAllowed().
bool KKeySequenceWidget::multiKeyShortcutsAllowed() const
Returns whether multikey shortcuts are allowed.
Note: Getter function for property multiKeyShortcutsAllowed.
See also setMultiKeyShortcutsAllowed().
[since 4.1]
void KKeySequenceWidget::setCheckActionCollections(const QList<KActionCollection *> &actionCollections)
Sets a list of actionCollections to check against for conflictuous shortcut.
If a QAction with a conflicting shortcut is found inside this list and its shortcut can be configured (KActionCollection::isShortcutConfigurable() returns true) the user will be prompted whether to steal the shortcut from this action.
This function was introduced in 4.1.
See also setCheckForConflictsAgainst().
[since 4.2]
void KKeySequenceWidget::setCheckForConflictsAgainst(KKeySequenceWidget::ShortcutTypes types)
Configure if the widget should check for conflicts with existing shortcut types.
When capturing a key sequence for local shortcuts you should check against GlobalShortcuts and your other local shortcuts. This is the default.
You have to provide the local actions to check against with setCheckActionCollections().
When capturing a key sequence for a global shortcut you should check against StandardShortcuts, GlobalShortcuts and your local shortcuts.
There are two ways to react to a user agreeing to steal a shortcut:
1. Listen to the stealShortcut() signal and steal the shortcuts manually. It's your responsibility to save that change later when you think it is appropriate.
2. Call applyStealShortcut() and KKeySequenceWidget will steal the shortcut. This will save the actionCollections the shortcut is part of so make sure it doesn't inadvertly save some unwanted changes too. Read its documentation for some limitation when handling global shortcuts.
If you want to do the conflict checking yourself here are some code snippets for global ...
QStringList conflicting = KGlobalAccel::findActionNameSystemwide(keySequence); if (!conflicting.isEmpty()) { // Inform and ask the user about the conflict and reassigning // the keys sequence if (!KGlobalAccel::promptStealShortcutSystemwide(q, conflicting, keySequence)) { return true; } KGlobalAccel::stealShortcutSystemwide(keySequence); }
... and standard shortcuts
KStandardShortcut::StandardShortcut ssc = KStandardShortcut::find(keySequence); if (ssc != KStandardShortcut::AccelNone) { // We have a conflict }
Note: Setter function for property checkForConflictsAgainst.
This function was introduced in 4.2.
See also checkForConflictsAgainst().
void KKeySequenceWidget::setClearButtonShown(bool show)
Sets whether to show a small button to set an empty key sequence next to the main input widget.
The default is to show the clear button.
void KKeySequenceWidget::setComponentName(const QString &componentName)
Sets the componentName for the component using this widget.
If the component using this widget supports shortcuts contexts, it must set its component name so we can check conflicts correctly.
[slot, since 5.65]
void KKeySequenceWidget::setKeySequence(const QKeySequence &seq, KKeySequenceWidget::Validation val = NoValidate)
Sets the key sequence seq.
If val == Validate, and the call is actually changing the key sequence, conflictuous shortcut will be checked.
Note: Setter function for property keySequence.
This function was introduced in 5.65.
See also keySequence().
[since 6.1]
void KKeySequenceWidget::setModifierOnlyAllowed(bool allow)
Sets whether to allow modifier-only key sequences.
Note: Setter function for property modifierOnlyAllowed.
This function was introduced in 6.1.
See also modifierOnlyAllowed().
void KKeySequenceWidget::setModifierlessAllowed(bool allow)
Sets whether to allow "plain" keys without modifiers (like Ctrl, Alt, Meta).
This only applies to user input, not to setKeySequence().
Plain keys by our definition include letter and symbol keys and text editing keys (Return, Space, Tab, Backspace, Delete).
"Special" keys like F1, Cursor keys, Insert, PageDown will always work.
Note: Setter function for property modifierlessAllowed.
See also isModifierlessAllowed().
void KKeySequenceWidget::setMultiKeyShortcutsAllowed(bool)
Sets whether to allow for multikey shortcuts.
Note: Setter function for property multiKeyShortcutsAllowed.
See also multiKeyShortcutsAllowed().
[signal]
void KKeySequenceWidget::stealShortcut(const QKeySequence &seq, QAction *action)
This signal is emitted after the user agreed to steal a shortcut sequence seq from an action.
This is only done for local shortcuts. So you can be sure action is one of the actions you provided with setCheckActionList() or setCheckActionCollections().
If you listen to that signal and don't call applyStealShortcut() you are supposed to steal the shortcut and save this change.