KGuiAddons

kkeysequencerecorder.h
1/*
2 SPDX-FileCopyrightText: 2001, 2002 Ellis Whitehead <ellis@kde.org>
3 SPDX-FileCopyrightText: 2007 Andreas Hartmetz <ahartmetz@gmail.com>
4 SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#ifndef KKEYSEQUENCERECORDER_H
10#define KKEYSEQUENCERECORDER_H
11
12#include <kguiaddons_export.h>
13
14#include <QKeySequence>
15#include <QObject>
16#include <QWindow>
17
18#include <memory>
19
20class KKeySequenceRecorderPrivate;
21
22/**
23 * @class KKeySequenceRecorder kkeysequencerecorder.h KKeySequenceRecorder
24 *
25 * @short Record a QKeySequence by listening to key events in a window.
26 *
27 * After calling startRecording key events in the set window will be captured until a valid
28 * QKeySequence has been recorded and gotKeySequence is emitted. See multiKeyShortcutsAllowed and
29 * modifierlessAllowed for what constitutes a valid key sequence.
30 *
31 * During recording any shortcuts are inhibited and cannot be triggered. Either by using the
32 * <a href="https://cgit.freedesktop.org/wayland/wayland-protocols/tree/unstable/keyboard-shortcuts-inhibit/keyboard-shortcuts-inhibit-unstable-v1.xml">
33 * keyboard-shortcuts-inhibit protocol </a> on Wayland or grabbing the keyboard.
34 *
35 * For graphical elements that record key sequences and can optionally perform conflict checking
36 * against existing shortcuts see KKeySequenceWidget and KeySequenceItem.
37 *
38 * Porting from KF5 to KF6:
39 *
40 * The class KeySequenceRecorder was renamed to KKeySequenceRecorder.
41 *
42 * @see KKeySequenceWidget, KeySequenceItem
43 * @since 6.0
44 */
45class KGUIADDONS_EXPORT KKeySequenceRecorder : public QObject
46{
48
49 /**
50 * Whether key events are currently recorded
51 */
52 Q_PROPERTY(bool isRecording READ isRecording NOTIFY recordingChanged)
53 /**
54 * The recorded key sequence.
55 * After construction this is empty.
56 *
57 * During recording it is continuously updated with the newest user input.
58 *
59 * After recording it contains the last recorded QKeySequence
60 */
61 Q_PROPERTY(QKeySequence currentKeySequence READ currentKeySequence WRITE setCurrentKeySequence NOTIFY currentKeySequenceChanged)
62 /**
63 * The window in which the key events are happening that should be recorded
64 */
65 Q_PROPERTY(QWindow *window READ window WRITE setWindow NOTIFY windowChanged)
66 /**
67 * If key presses of "plain" keys without a modifier are considered to be a valid finished
68 * key combination.
69 * Plain keys include letter and symbol keys and text editing keys (Return, Space, Tab,
70 * Backspace, Delete). Other keys like F1, Cursor keys, Insert, PageDown will always work.
71 *
72 * By default this is `false`.
73 *
74 * @deprecated since 6.12, use the patterns property instead.
75 */
76 Q_PROPERTY(bool modifierlessAllowed READ modifierlessAllowed WRITE setModifierlessAllowed NOTIFY modifierlessAllowedChanged)
77 /** Controls the amount of key combinations that are captured until recording stops and gotKeySequence
78 * is emitted.
79 * By default this is `true` and "Emacs-style" key sequences are recorded. Recording does not
80 * stop until four valid key combination have been recorded. Afterwards `currentKeySequence().count()`
81 * will be 4.
82 *
83 * Otherwise only one key combination is recorded before gotKeySequence is emitted with a
84 * QKeySequence with a `count()` of 1.
85 * @see QKeySequence
86 */
87 Q_PROPERTY(bool multiKeyShortcutsAllowed READ multiKeyShortcutsAllowed WRITE setMultiKeyShortcutsAllowed NOTIFY multiKeyShortcutsAllowedChanged)
88
89 /**
90 * It makes it acceptable for the key sequence to be just a modifier (e.g. Shift or Control)
91 *
92 * By default, if only a modifier is pressed and then released, the component will remain waiting for the sequence.
93 * When enabled, it will take the modifier key as the key sequence.
94 *
95 * By default this is `false`.
96 *
97 * @deprecated since 6.12, use the patterns property instead.
98 */
99 Q_PROPERTY(bool modifierOnlyAllowed READ modifierOnlyAllowed WRITE setModifierOnlyAllowed NOTIFY modifierOnlyAllowedChanged)
100
101 /**
102 * Specifies what components the recorded shortcut must have, for example whether the shortcut
103 * must contain only modifier keys (`Modifier`) or modifiers keys and a normal key (`ModifierAndKey`).
104 *
105 * The patterns property can contain one or more recording patterns. For example, if the recorder
106 * accepts both normal and modifier only shortcuts, e.g. `Modifier | ModifierAndKey`.
107 *
108 * By default this is `ModifierAndKey`.
109 *
110 * @since 6.12
111 */
112 Q_PROPERTY(Patterns patterns READ patterns WRITE setPatterns NOTIFY patternsChanged)
113
114public:
115 /**
116 * The Pattern type specifies what components the recorded shortcut must have, e.g. modifiers or just a key.
117 */
118 enum Pattern {
119 /**
120 * The recorded shortcut must contain one or more modifier keys (Meta, Shift, Ctrl, or Alt).
121 */
122 Modifier = 0x1,
123 /**
124 * The recorded shortcut must contain only one regular key, e.g. "A".
125 */
126 Key = 0x2,
127 /**
128 * The recorded shortcut must contain one or more modifier keys followed by a regular key,
129 * e.g. Meta+A.
130 *
131 * Note: with this pattern, special keys like "Insert" without any pressed modifier will be
132 * captured too. Please do not rely on this behavior and instead use `Key | ModifierAndKey`
133 * explicitly. The future versions of KKeySequenceRecorder are expected not to record special
134 * keys in the ModifierAndKey mode.
135 */
137 };
138 Q_DECLARE_FLAGS(Patterns, Pattern)
139 Q_FLAG(Patterns)
140
141 /**
142 * Constructor.
143 *
144 * @par window The window whose key events will be recorded.
145 * @see window
146 */
147 explicit KKeySequenceRecorder(QWindow *window, QObject *parent = nullptr);
148 ~KKeySequenceRecorder() override;
149
150 /**
151 * Start recording.
152 * Calling startRecording when window() is `nullptr` has no effect.
153 */
154 Q_INVOKABLE void startRecording();
155
156 bool isRecording() const;
157
158 QKeySequence currentKeySequence() const;
159 void setCurrentKeySequence(const QKeySequence &sequence);
160
161 QWindow *window() const;
162 void setWindow(QWindow *window);
163
164 bool multiKeyShortcutsAllowed() const;
165 void setMultiKeyShortcutsAllowed(bool allowed);
166
167#if KGUIADDONS_ENABLE_DEPRECATED_SINCE(6, 12)
168 /**
169 * @deprecated since 6.12, use setPatterns() instead
170 */
171 KGUIADDONS_DEPRECATED_VERSION(6, 12, "use setPatterns() instead") void setModifierlessAllowed(bool allowed);
172
173 /**
174 * @deprecated since 6.12, use patterns() instead
175 */
176 KGUIADDONS_DEPRECATED_VERSION(6, 12, "use patterns() instead") bool modifierlessAllowed() const;
177
178 /**
179 * @deprecated since 6.12, use setPatterns() instead
180 */
181 KGUIADDONS_DEPRECATED_VERSION(6, 12, "use setPatterns() instead") void setModifierOnlyAllowed(bool allowed);
182
183 /**
184 * @deprecated since 6.12, use patterns() instead
185 */
186 KGUIADDONS_DEPRECATED_VERSION(6, 12, "use patterns() instead") bool modifierOnlyAllowed() const;
187#endif
188
189 void setPatterns(Patterns patterns);
190 Patterns patterns() const;
191
192public Q_SLOTS:
193 /**
194 * Stops the recording session
195 */
196 void cancelRecording();
197
198Q_SIGNALS:
199 /**
200 * This signal is emitted when a key sequence has been recorded.
201 *
202 * Compared to currentKeySequenceChanged and currentKeySequence this is signal is not emitted
203 * continuously during recording but only after recording has finished.
204 */
205 void gotKeySequence(const QKeySequence &keySequence);
206
207 void recordingChanged();
208 void windowChanged();
209 void currentKeySequenceChanged();
210 void multiKeyShortcutsAllowedChanged();
211#if KGUIADDONS_ENABLE_DEPRECATED_SINCE(6, 12)
212 KGUIADDONS_DEPRECATED_VERSION(6, 12, "use patternsChanged() instead") void modifierlessAllowedChanged();
213 KGUIADDONS_DEPRECATED_VERSION(6, 12, "use patternsChanged() instead") void modifierOnlyAllowedChanged();
214#endif
215 void patternsChanged();
216
217private:
218 friend class KKeySequenceRecorderPrivate;
219 std::unique_ptr<KKeySequenceRecorderPrivate> const d;
220};
221
222Q_DECLARE_OPERATORS_FOR_FLAGS(KKeySequenceRecorder::Patterns)
223
224#endif
Record a QKeySequence by listening to key events in a window.
Patterns patterns
Specifies what components the recorded shortcut must have, for example whether the shortcut must cont...
bool multiKeyShortcutsAllowed
Controls the amount of key combinations that are captured until recording stops and gotKeySequence is...
void gotKeySequence(const QKeySequence &keySequence)
This signal is emitted when a key sequence has been recorded.
bool modifierOnlyAllowed
It makes it acceptable for the key sequence to be just a modifier (e.g.
KKeySequenceRecorder(QWindow *window, QObject *parent=nullptr)
Constructor.
QKeySequence currentKeySequence
The recorded key sequence.
bool isRecording
Whether key events are currently recorded.
QWindow * window
The window in which the key events are happening that should be recorded.
Pattern
The Pattern type specifies what components the recorded shortcut must have, e.g.
@ Modifier
The recorded shortcut must contain one or more modifier keys (Meta, Shift, Ctrl, or Alt).
@ Key
The recorded shortcut must contain only one regular key, e.g.
@ ModifierAndKey
The recorded shortcut must contain one or more modifier keys followed by a regular key,...
bool modifierlessAllowed
If key presses of "plain" keys without a modifier are considered to be a valid finished key combinati...
QObject(QObject *parent)
Q_FLAG(...)
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 11:55:06 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.