Skip to content

PythonQt.QtGui.QKeySequence

QKeySequence

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

In its most common form, a key sequence describes a combination of keys that must be used together to perform some action. Key sequences are used with QAction objects to specify which keyboard shortcuts can be used to trigger actions.

Key sequences can be constructed for use as keyboard shortcuts in three different ways:

  • For standard shortcuts, a standard key can be used to request the platform-specific key sequence associated with each shortcut.
  • For custom shortcuts, human-readable strings such as "Ctrl+X" can be used, and these can be translated into the appropriate shortcuts for users of different languages. Translations are made in the "QShortcut" context.
  • For hard-coded shortcuts, integer key codes can be specified with a combination of values defined by the Qt::Key and Qt::Modifier enum values. Each key code consists of a single Qt::Key value and zero or more modifiers, such as Qt::SHIFT, Qt::CTRL, Qt::ALT and Qt::META.

For example, Ctrl P might be a sequence used as a shortcut for printing a document, and can be specified in any of the following ways:

QKeySequence(QKeySequence::Print);
QKeySequence(tr("Ctrl+P"));
QKeySequence(tr("Ctrl+p"));
QKeySequence(Qt::CTRL + Qt::Key_P);

Note that, for letters, the case used in the specification string does not matter. In the above examples, the user does not need to hold down the Shift key to activate a shortcut specified with "Ctrl+P". However, for other keys, the use of Shift as an unspecified extra modifier key can lead to confusion for users of an application whose keyboards have different layouts to those used by the developers. See the Keyboard Layout Issues section below for more details.

It is preferable to use standard shortcuts where possible. When creating key sequences for non-standard shortcuts, you should use human-readable strings in preference to hard-coded integer values.

QKeySequence objects can be cast to a QString to obtain a human-readable translated version of the sequence. Similarly, the toString() function produces human-readable strings for use in menus. On macOS, the appropriate symbols are used to describe keyboard shortcuts using special keys on the Macintosh keyboard.

An alternative way to specify hard-coded key codes is to use the Unicode code point of the character; for example, 'A' gives the same key sequence as Qt::Key_A.

Note: On macOS, references to "Ctrl", Qt::CTRL, Qt::Key_Control and Qt::ControlModifier correspond to the Command keys on the Macintosh keyboard, and references to "Meta", Qt::META, Qt::Key_Meta and Qt::MetaModifier correspond to the Control keys. Developers on macOS can use the same shortcut descriptions across all platforms, and their applications will automatically work as expected on macOS.

Standard Shortcuts

QKeySequence defines many standard keyboard shortcuts to reduce the amount of effort required when setting up actions in a typical application. The table below shows some common key sequences that are often used for these standard shortcuts by applications on four widely-used platforms. Note that on macOS, the Ctrl value corresponds to the Command keys on the Macintosh keyboard, and the Meta value corresponds to the Control keys.

StandardKey Windows macOS KDE Plasma GNOME
HelpContents F1 Ctrl+? F1 F1
WhatsThis Shift+F1 Shift+F1 Shift+F1 Shift+F1
Open Ctrl+O Ctrl+O Ctrl+O Ctrl+O
Close Ctrl+F4, Ctrl+W Ctrl+W, Ctrl+F4 Ctrl+W Ctrl+W
Save Ctrl+S Ctrl+S Ctrl+S Ctrl+S
Quit Ctrl+Q Ctrl+Q Ctrl+Q
SaveAs Ctrl+Shift+S Ctrl+Shift+S
New Ctrl+N Ctrl+N Ctrl+N Ctrl+N
Delete Del Del, Meta+D Del, Ctrl+D Del, Ctrl+D
Cut Ctrl+X, Shift+Del Ctrl+X, Meta+K Ctrl+X, F20, Shift+Del Ctrl+X, F20, Shift+Del
Copy Ctrl+C, Ctrl+Ins Ctrl+C Ctrl+C, F16, Ctrl+Ins Ctrl+C, F16, Ctrl+Ins
Paste Ctrl+V, Shift+Ins Ctrl+V, Meta+Y Ctrl+V, F18, Shift+Ins Ctrl+V, F18, Shift+Ins
Preferences Ctrl+,
Undo Ctrl+Z, Alt+Backspace Ctrl+Z Ctrl+Z, F14 Ctrl+Z, F14
Redo Ctrl+Y, Shift+Ctrl+Z, Alt+Shift+Backspace Ctrl+Shift+Z Ctrl+Shift+Z Ctrl+Shift+Z
Back Alt+Left, Backspace Ctrl+[ Alt+Left Alt+Left
Forward Alt+Right, Shift+Backspace Ctrl+] Alt+Right Alt+Right
Refresh F5 F5 F5 Ctrl+R, F5
ZoomIn Ctrl+Plus Ctrl+Plus Ctrl+Plus Ctrl+Plus
ZoomOut Ctrl+Minus Ctrl+Minus Ctrl+Minus Ctrl+Minus
FullScreen F11, Alt+Enter Ctrl+Meta+F F11, Ctrl+Shift+F Ctrl+F11
Print Ctrl+P Ctrl+P Ctrl+P Ctrl+P
AddTab Ctrl+T Ctrl+T Ctrl+Shift+N, Ctrl+T Ctrl+T
NextChild Ctrl+Tab, Forward, Ctrl+F6 Ctrl+}, Forward, Ctrl+Tab Ctrl+Tab, Forward, Ctrl+Comma Ctrl+Tab, Forward
PreviousChild Ctrl+Shift+Tab, Back, Ctrl+Shift+F6 Ctrl+{, Back, Ctrl+Shift+Tab Ctrl+Shift+Tab, Back, Ctrl+Period Ctrl+Shift+Tab, Back
Find Ctrl+F Ctrl+F Ctrl+F Ctrl+F
FindNext F3, Ctrl+G Ctrl+G F3 Ctrl+G, F3
FindPrevious Shift+F3, Ctrl+Shift+G Ctrl+Shift+G Shift+F3 Ctrl+Shift+G, Shift+F3
Replace Ctrl+H (none) Ctrl+R Ctrl+H
SelectAll Ctrl+A Ctrl+A Ctrl+A Ctrl+A
Deselect Ctrl+Shift+A Ctrl+Shift+A
Bold Ctrl+B Ctrl+B Ctrl+B Ctrl+B
Italic Ctrl+I Ctrl+I Ctrl+I Ctrl+I
Underline Ctrl+U Ctrl+U Ctrl+U Ctrl+U
MoveToNextChar Right Right, Meta+F Right Right
MoveToPreviousChar Left Left, Meta+B Left Left
MoveToNextWord Ctrl+Right Alt+Right Ctrl+Right Ctrl+Right
MoveToPreviousWord Ctrl+Left Alt+Left Ctrl+Left Ctrl+Left
MoveToNextLine Down Down, Meta+N Down Down
MoveToPreviousLine Up Up, Meta+P Up Up
MoveToNextPage PgDown PgDown, Alt+PgDown, Meta+Down, Meta+PgDown, Meta+V PgDown PgDown
MoveToPreviousPage PgUp PgUp, Alt+PgUp, Meta+Up, Meta+PgUp PgUp PgUp
MoveToStartOfLine Home Ctrl+Left, Meta+Left Home Home
MoveToEndOfLine End Ctrl+Right, Meta+Right End, Ctrl+E End, Ctrl+E
MoveToStartOfBlock (none) Alt+Up, Meta+A (none) (none)
MoveToEndOfBlock (none) Alt+Down, Meta+E (none) (none)
MoveToStartOfDocument Ctrl+Home Ctrl+Up, Home Ctrl+Home Ctrl+Home
MoveToEndOfDocument Ctrl+End Ctrl+Down, End Ctrl+End Ctrl+End
SelectNextChar Shift+Right Shift+Right Shift+Right Shift+Right
SelectPreviousChar Shift+Left Shift+Left Shift+Left Shift+Left
SelectNextWord Ctrl+Shift+Right Alt+Shift+Right Ctrl+Shift+Right Ctrl+Shift+Right
SelectPreviousWord Ctrl+Shift+Left Alt+Shift+Left Ctrl+Shift+Left Ctrl+Shift+Left
SelectNextLine Shift+Down Shift+Down Shift+Down Shift+Down
SelectPreviousLine Shift+Up Shift+Up Shift+Up Shift+Up
SelectNextPage Shift+PgDown Shift+PgDown Shift+PgDown Shift+PgDown
SelectPreviousPage Shift+PgUp Shift+PgUp Shift+PgUp Shift+PgUp
SelectStartOfLine Shift+Home Ctrl+Shift+Left Shift+Home Shift+Home
SelectEndOfLine Shift+End Ctrl+Shift+Right Shift+End Shift+End
SelectStartOfBlock (none) Alt+Shift+Up, Meta+Shift+A (none) (none)
SelectEndOfBlock (none) Alt+Shift+Down, Meta+Shift+E (none) (none)
SelectStartOfDocument Ctrl+Shift+Home Ctrl+Shift+Up, Shift+Home Ctrl+Shift+Home Ctrl+Shift+Home
SelectEndOfDocument Ctrl+Shift+End Ctrl+Shift+Down, Shift+End Ctrl+Shift+End Ctrl+Shift+End
DeleteStartOfWord Ctrl+Backspace Alt+Backspace Ctrl+Backspace Ctrl+Backspace
DeleteEndOfWord Ctrl+Del (none) Ctrl+Del Ctrl+Del
DeleteEndOfLine (none) (none) Ctrl+K Ctrl+K
DeleteCompleteLine (none) (none) Ctrl+U Ctrl+U
InsertParagraphSeparator Enter Enter Enter Enter
InsertLineSeparator Shift+Enter Meta+Enter, Meta+O Shift+Enter Shift+Enter
Backspace (none) Meta+H (none) (none)
Cancel Escape Escape, Ctrl+. Escape Escape

Note that, since the key sequences used for the standard shortcuts differ between platforms, you still need to test your shortcuts on each platform to ensure that you do not unintentionally assign the same key sequence to many actions.

Keyboard Layout Issues

Many key sequence specifications are chosen by developers based on the layout of certain types of keyboard, rather than choosing keys that represent the first letter of an action's name, such as Ctrl S ("Ctrl+S") or Ctrl C ("Ctrl+C"). Additionally, because certain symbols can only be entered with the help of modifier keys on certain keyboard layouts, key sequences intended for use with one keyboard layout may map to a different key, map to no keys at all, or require an additional modifier key to be used on different keyboard layouts.

For example, the shortcuts, Ctrl plus and Ctrl minus, are often used as shortcuts for zoom operations in graphics applications, and these may be specified as "Ctrl++" and "Ctrl+-" respectively. However, the way these shortcuts are specified and interpreted depends on the keyboard layout. Users of Norwegian keyboards will note that the + and - keys are not adjacent on the keyboard, but will still be able to activate both shortcuts without needing to press the Shift key. However, users with British keyboards will need to hold down the Shift key to enter the + symbol, making the shortcut effectively the same as "Ctrl+Shift+\=".

Although some developers might resort to fully specifying all the modifiers they use on their keyboards to activate a shortcut, this will also result in unexpected behavior for users of different keyboard layouts.

For example, a developer using a British keyboard may decide to specify "Ctrl+Shift+\=" as the key sequence in order to create a shortcut that coincidentally behaves in the same way as Ctrl plus. However, the \= key needs to be accessed using the Shift key on Norwegian keyboard, making the required shortcut effectively Ctrl Shift Shift \= (an impossible key combination).

As a result, both human-readable strings and hard-coded key codes can both be problematic to use when specifying a key sequence that can be used on a variety of different keyboard layouts. Only the use of standard shortcuts guarantees that the user will be able to use the shortcuts that the developer intended.

Despite this, we can address this issue by ensuring that human-readable strings are used, making it possible for translations of key sequences to be made for users of different languages. This approach will be successful for users whose keyboards have the most typical layout for the language they are using.

GNU Emacs Style Key Sequences

Key sequences similar to those used in GNU Emacs, allowing up to four key codes, can be created by using the multiple argument constructor, or by passing a human-readable string of comma-separated key sequences.

For example, the key sequence, Ctrl X followed by Ctrl C, can be specified using either of the following ways:

QKeySequence(tr("Ctrl+X, Ctrl+C"));
QKeySequence(Qt::CTRL + Qt::Key_X, Qt::CTRL + Qt::Key_C);

Warning: A QApplication instance must have been constructed before a QKeySequence is created; otherwise, your application may crash.

Source: https://doc.qt.io/archives/qt-5.15/qkeysequence.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
AddTab attribute Any
Back attribute Any
Backspace attribute Any
Bold attribute Any (shared captured text)
Cancel attribute Any (shared captured text)
Close attribute Any (shared captured text)
Copy attribute Any
Cut attribute Any
Delete attribute Any
DeleteCompleteLine attribute Any
DeleteEndOfLine attribute Any
DeleteEndOfWord attribute Any
DeleteStartOfWord attribute Any
Deselect attribute Any (shared captured text)
ExactMatch attribute Any
Find attribute Any
FindNext attribute Any
FindPrevious attribute Any
Forward attribute Any (shared captured text)
FullScreen attribute Any
HelpContents attribute Any
InsertLineSeparator attribute Any
InsertParagraphSeparator attribute Any
Italic attribute Any
MoveToEndOfBlock attribute Any
MoveToEndOfDocument attribute Any
MoveToEndOfLine attribute Any
MoveToNextChar attribute Any
MoveToNextLine attribute Any
MoveToNextPage attribute Any
MoveToNextWord attribute Any
MoveToPreviousChar attribute Any
MoveToPreviousLine attribute Any
MoveToPreviousPage attribute Any
MoveToPreviousWord attribute Any
MoveToStartOfBlock attribute Any
MoveToStartOfDocument attribute Any
MoveToStartOfLine attribute Any
NativeText attribute Any
New attribute Any
NextChild attribute Any
NoMatch attribute Any (shared captured text)
Open attribute Any (shared captured text)
PartialMatch attribute Any
Paste attribute Any
PortableText attribute Any
Preferences attribute Any
PreviousChild attribute Any
Print attribute Any
Quit attribute Any (shared captured text)
Redo attribute Any
Refresh attribute Any
Replace attribute Any
Save attribute Any (shared captured text)
SaveAs attribute Any
SelectAll attribute Any
SelectEndOfBlock attribute Any
SelectEndOfDocument attribute Any
SelectEndOfLine attribute Any
SelectNextChar attribute Any
SelectNextLine attribute Any
SelectNextPage attribute Any
SelectNextWord attribute Any
SelectPreviousChar attribute Any
SelectPreviousLine attribute Any
SelectPreviousPage attribute Any
SelectPreviousWord attribute Any
SelectStartOfBlock attribute Any
SelectStartOfDocument attribute Any
SelectStartOfLine attribute Any
SequenceFormat attribute type Convert a number or string to an integer, or return 0 if no arguments are given.
SequenceMatch attribute type Convert a number or string to an integer, or return 0 if no arguments are given.
StandardKey attribute type Convert a number or string to an integer, or return 0 if no arguments are given.
Underline attribute Any (shared captured text)
Undo attribute Any
UnknownKey attribute Any
WhatsThis attribute Any (shared captured text)
ZoomIn attribute Any
ZoomOut attribute Any
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
count function (self) -> int X.count() -> int
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
fromString function (self, str: str, format: Any) -> Any X.fromString(str, format) -> PythonQt.QtGui.QKeySequence
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
isEmpty function (self) -> bool X.isEmpty() -> bool
keyBindings function (self, key: Any) -> tuple X.keyBindings(key) -> tuple
listFromString function (self, str: str, format: Any) -> tuple X.listFromString(str, format) -> tuple
listToString function (self, list: Any, format: Any) -> str X.listToString(list, format) -> str
matches function (self, seq: Any) -> Any Matches the sequence with seq.
mnemonic function (self, text: str) -> Any X.mnemonic(text) -> PythonQt.QtGui.QKeySequence
operator_subscript function (self, i: Any) -> int X.operator_subscript(i) -> int
readFrom function (self, out: Any) -> Any
swap function (self, other: Any) -> None Swaps key sequence other with this key sequence.
toString function (self, format: Any) -> str X.toString(format) -> str
writeTo function (self, *args: Any, **kwargs: Any) -> Any