Pages
API Documentation
On this page

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

List of all members, including inherited members
Deprecated members

Public Types

Properties

Public Functions

Public Slots

Signals

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, 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:

keySequence : QKeySequence

Access functions:

  • QKeySequence keySequence() const
  • void setKeySequence(const QKeySequence &seq, KKeySequenceWidget::Validation val = NoValidate)

Notifier signal:

modifierOnlyAllowed : bool

multiKeyShortcutsAllowed : bool

Access functions:

[since 6.12] patterns : KKeySequenceRecorder::Patterns

Specifies the accepted shortcut formats.

Default is KKeySequenceRecorder::ModifierAndKey.

This property was introduced in Qt 6.12.

Access functions:

  • KKeySequenceRecorder::Patterns patterns() const
  • void setPatterns(KKeySequenceRecorder::Patterns patterns)

[read-only, since 6.12] recording : bool

Indicates whether a key sequence is currently being recorded.

This property was introduced in Qt 6.12.

Access functions:

Notifier signal:

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()

KKeySequenceWidget::ShortcutTypes KKeySequenceWidget::checkForConflictsAgainst() const

The shortcut types we check for conflicts.

Note: Getter function for property checkForConflictsAgainst.

See also setCheckForConflictsAgainst()

[slot] void KKeySequenceWidget::clearKeySequence()

Clears the key sequence.

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.

[since 6.12] bool KKeySequenceWidget::isRecording() const

Returns true if a key sequence is currently being recorded; otherwise returns false.

Note: Getter function for property recording.

This function was introduced in Qt 6.12.

QKeySequence KKeySequenceWidget::keySequence() const

Returns the currently selected key sequence.

Note: Getter function for property keySequence.

See also setKeySequence()

[signal] 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.

bool KKeySequenceWidget::multiKeyShortcutsAllowed() const

Returns whether multikey shortcuts are allowed.

Note: Getter function for property multiKeyShortcutsAllowed.

See also setMultiKeyShortcutsAllowed()

[since 6.12] KKeySequenceRecorder::Patterns KKeySequenceWidget::patterns() const

Returns the accepted shortcut patterns.

Default is Modifier | Key

Note: Getter function for property patterns.

This function was introduced in Qt 6.12.

See also setPatterns()

[signal, since 6.12] void KKeySequenceWidget::recordingChanged()

This signal is emitted when the user begins or finishes recording a key sequence. It is not emitted when the current key sequence is changed using setKeySequence().

Note: Notifier signal for property recording.

This function was introduced in Qt 6.12.

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.

See also setCheckForConflictsAgainst()

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.

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] 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.

See also keySequence()

void KKeySequenceWidget::setMultiKeyShortcutsAllowed(bool)

Sets whether to allow for multikey shortcuts.

Note: Setter function for property multiKeyShortcutsAllowed.

See also multiKeyShortcutsAllowed()

[since 6.12] void KKeySequenceWidget::setPatterns(KKeySequenceRecorder::Patterns patterns)

Sets the accepted shortcut patterns to patterns. A shortcut pattern specifies what components the recoreded shortcut must have, e.g. whether it should include modifier keys, etc.

Note: Setter function for property patterns.

This function was introduced in Qt 6.12.

See also patterns()

[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.

We use cookies to personalize content and to analyze our traffic. Please decide if you are willing to accept cookies from our website.