Home · All Classes · Modules

QKeySequence Class Reference
[QtGui module]

The QKeySequence class encapsulates a key sequence as used by shortcuts. More...

Types

Methods

Static Methods

Special Methods


Detailed Description

A Python int object or a QString may be used whenever a QKeySequence is expected.

The QKeySequence class encapsulates a key sequence as used by shortcuts.

A key sequence consists of up to four keyboard codes, each optionally combined with modifiers, such as Qt.SHIFT, Qt.CTRL, Qt.ALT or Qt.META. For example, Qt.CTRL + Qt.Key_P might be a sequence used as a shortcut for printing a document. Valid codes for keys and modifiers are listed in Qt.Key and Qt.Modifier. As an alternative, use the unicode code point of the character; for example, 'A' gives the same key sequence as Qt.Key_A.

Key sequences can be constructed either from an integer key code, or from a human readable translatable string such as "Ctrl+X,Alt+Space". A key sequence can be cast to a QString to obtain a human readable translated version of the sequence. Translations are done in the "QShortcut" context.

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

The toString() function produces human-readable strings for use in menus. On Mac OS X, the appropriate symbols are used to describe keyboard shortcuts using special keys on the Macintosh keyboard.

Standard Shortcuts

QKeySequence defines many StandardKey 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 Mac OS X, the Ctrl value corresponds to the Command keys on the Macintosh keyboard, and the Meta value corresponds to the Control keys.

StandardKey Windows Mac OS X KDE 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
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 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 Ctrl+V, F18, Shift+Ins Ctrl+V, F18, Shift+Ins
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+Y 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
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, F3
Replace Ctrl+H (none) Ctrl+R Ctrl+H
SelectAll Ctrl+A Ctrl+A Ctrl+A Ctrl+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 Right Right
MoveToPreviousChar Left Left Left Left
MoveToNextWord Ctrl+Right Alt+Right Ctrl+Right Ctrl+Right
MoveToPreviousWord Ctrl+Left Alt+Left Ctrl+Left Ctrl+Left
MoveToNextLine Down Down Down Down
MoveToPreviousLine Up Up Up Up
MoveToNextPage PgDown PgDown, Alt+PgDown, Meta+Down, Meta+PgDown 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 End
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 (none) (none)
SelectEndOfBlock (none) Alt+Shift+Down (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


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.

See also QShortcut.


Type Documentation

QKeySequence.SequenceFormat

Constant Value Description
QKeySequence.NativeText 0 The key sequence as a platform specific string. This means that it will be shown translated and on the Mac it will resemble a keysequence from the menu bar. This enum is best used when you want to display the string to the user.
QKeySequence.PortableText 1 The key sequence is given in a "portable" format, suitable for reading and writing to a file. In many cases, it will look similar to the native text on Windows and X11.


QKeySequence.SequenceMatch

Constant Value Description
QKeySequence.NoMatch 0 The key sequences are different; not even partially matching.
QKeySequence.PartialMatch 1 The key sequences match partially, but are not the same.
QKeySequence.ExactMatch 2 The key sequences are the same.


QKeySequence.StandardKey

This enum represent standard key bindings. They can be used to assign platform dependent keyboard shortcuts to a QAction. QKeyEvent also provides the function QKeyEvent.standardKey() to query if it matches an existing key binding.

Note that the key bindings are platform dependent. The currently bound shortcuts can be queried using keyBindings().

Constant Value Description
QKeySequence.UnknownKey 0 Unbound key.
QKeySequence.HelpContents 1 Open help contents.
QKeySequence.WhatsThis 2 Activate whats this.
QKeySequence.Open 3 Open Document.
QKeySequence.Close 4 Close Document/Tab.
QKeySequence.Save 5 Save Document.
QKeySequence.New 6 Create new Document.
QKeySequence.Delete 7 Delete.
QKeySequence.Cut 8 Cut.
QKeySequence.Copy 9 Copy.
QKeySequence.Paste 10 Paste.
QKeySequence.Undo 11 Undo.
QKeySequence.Redo 12 Redo.
QKeySequence.Back 13 Navigate back.
QKeySequence.Forward 14 Navigate forward.
QKeySequence.Refresh 15 Refresh or reload current document.
QKeySequence.ZoomIn 16 Zoom in.
QKeySequence.ZoomOut 17 Zoom out.
QKeySequence.Print 18 Print document.
QKeySequence.AddTab 19 Add new tab.
QKeySequence.NextChild 20 Navigate to next tab or child window.
QKeySequence.PreviousChild 21 Navigate to previous tab or child window.
QKeySequence.Find 22 Find in document.
QKeySequence.FindNext 23 Find next result.
QKeySequence.FindPrevious 24 Find previous result.
QKeySequence.Replace 25 Find and replace.
QKeySequence.SelectAll 26 Select all text.
QKeySequence.Bold 27 Bold text.
QKeySequence.Italic 28 Italic text.
QKeySequence.Underline 29 Underline text.
QKeySequence.MoveToNextChar 30 Move cursor to next character.
QKeySequence.MoveToPreviousChar 31 Move cursor to previous character.
QKeySequence.MoveToNextWord 32 Move cursor to next word.
QKeySequence.MoveToPreviousWord 33 Move cursor to previous word.
QKeySequence.MoveToNextLine 34 Move cursor to next line.
QKeySequence.MoveToPreviousLine 35 Move cursor to previous line.
QKeySequence.MoveToNextPage 36 Move cursor to next page.
QKeySequence.MoveToPreviousPage 37 Move cursor to previous page.
QKeySequence.MoveToStartOfLine 38 Move cursor to start of line.
QKeySequence.MoveToEndOfLine 39 Move cursor to end of line.
QKeySequence.MoveToStartOfBlock 40 Move cursor to start of a block. This shortcut is only used on OS X.
QKeySequence.MoveToEndOfBlock 41 Move cursor to end of block. This shortcut is only used on the OS X.
QKeySequence.MoveToStartOfDocument 42 Move cursor to start of document.
QKeySequence.MoveToEndOfDocument 43 Move cursor to end of document.
QKeySequence.SelectNextChar 44 Extend selection to next character.
QKeySequence.SelectPreviousChar 45 Extend selection to previous character.
QKeySequence.SelectNextWord 46 Extend selection to next word.
QKeySequence.SelectPreviousWord 47 Extend selection to previous word.
QKeySequence.SelectNextLine 48 Extend selection to next line.
QKeySequence.SelectPreviousLine 49 Extend selection to previous line.
QKeySequence.SelectNextPage 50 Extend selection to next page.
QKeySequence.SelectPreviousPage 51 Extend selection to previous page.
QKeySequence.SelectStartOfLine 52 Extend selection to start of line.
QKeySequence.SelectEndOfLine 53 Extend selection to end of line.
QKeySequence.SelectStartOfBlock 54 Extend selection to the start of a text block. This shortcut is only used on OS X.
QKeySequence.SelectEndOfBlock 55 Extend selection to the end of a text block. This shortcut is only used on OS X.
QKeySequence.SelectStartOfDocument 56 Extend selection to start of document.
QKeySequence.SelectEndOfDocument 57 Extend selection to end of document.
QKeySequence.DeleteStartOfWord 58 Delete the beginning of a word up to the cursor.
QKeySequence.DeleteEndOfWord 59 Delete word from the end of the cursor.
QKeySequence.DeleteEndOfLine 60 Delete end of line.


This enum was introduced in Qt 4.2.


Method Documentation

QKeySequence.__init__ (self)

Constructs an empty key sequence.

QKeySequence.__init__ (self, QString key)

Creates a key sequence from the key string. For example "Ctrl+O" gives CTRL+'O'. The strings "Ctrl", "Shift", "Alt" and "Meta" are recognized, as well as their translated equivalents in the "QShortcut" context (using QObject.tr()).

Up to four key codes may be entered by separating them with commas, e.g. "Alt+X,Ctrl+S,Q".

This constructor is typically used with tr(), so that shortcut keys can be replaced in translations:

 QMenu *file = new QMenu(this);
 file->addAction(tr("&Open..."), this, SLOT(open()),
                   QKeySequence(tr("Ctrl+O", "File|Open")));

Note the "File|Open" translator comment. It is by no means necessary, but it provides some context for the human translator.

QKeySequence.__init__ (self, int k1, int k2 = 0, int k3 = 0, int k4 = 0)

Constructs a key sequence with up to 4 keys k1, k2, k3 and k4.

The key codes are listed in Qt.Key and can be combined with modifiers (see Qt.Modifier) such as Qt.SHIFT, Qt.CTRL, Qt.ALT, or Qt.META.

QKeySequence.__init__ (self, QKeySequence ks)

Copy constructor. Makes a copy of keysequence.

QKeySequence.__init__ (self, StandardKey key)

Constructs a QKeySequence object for the given key. The result will depend on the currently running platform.

The resulting object will be based on the first element in the list of key bindings for the key.

This function was introduced in Qt 4.2.

QKeySequence.__init__ (self, QVariant variant)

unsigned long QKeySequence.count (self)

Returns the number of keys in the key sequence. The maximum is 4.

QKeySequence QKeySequence.fromString (QString str, SequenceFormat format = QKeySequence.PortableText)

Return a QKeySequence from the string str based on format.

This function was introduced in Qt 4.1.

See also toString().

bool QKeySequence.isDetached (self)

bool QKeySequence.isEmpty (self)

Returns true if the key sequence is empty; otherwise returns false.

QKeySequence-list QKeySequence.keyBindings (StandardKey key)

Returns a list of key bindings for the given key. The result of calling this function will vary based on the target platform. The first element of the list indicates the primary shortcut for the given platform. If the result contains more than one result, these can be considered alternative shortcuts on the same platform for the given key.

This function was introduced in Qt 4.2.

SequenceMatch QKeySequence.matches (self, QKeySequence seq)

Matches the sequence with seq. Returns ExactMatch if successful, PartialMatch if seq matches incompletely, and NoMatch if the sequences have nothing in common. Returns NoMatch if seq is shorter.

QKeySequence QKeySequence.mnemonic (QString text)

Returns the shortcut key sequence for the mnemonic in text, or an empty key sequence if no mnemonics are found.

For example, mnemonic("E&xit") returns Qt.ALT+Qt.Key_X, mnemonic("&Quit") returns ALT+Key_Q, and mnemonic("Quit") returns an empty QKeySequence.

We provide a list of common mnemonics in English. At the time of writing, Microsoft and Open Group do not appear to have issued equivalent recommendations for other languages.

QString QKeySequence.toString (self, SequenceFormat format = QKeySequence.PortableText)

Return a string representation of the key sequence, based on format.

For example, the value Qt.CTRL+Qt.Key_O results in "Ctrl+O". If the key sequence has multiple key codes, each is separated by commas in the string returned, such as "Alt+X, Ctrl+Y, Z". The strings, "Ctrl", "Shift", etc. are translated using QObject.tr() in the "QShortcut" context.

If the key sequence has no keys, an empty string is returned.

On Mac OS X, the string returned resembles the sequence that is shown in the menubar.

This function was introduced in Qt 4.1.

See also fromString().

bool QKeySequence.__eq__ (self, QKeySequence other)

bool QKeySequence.__ge__ (self, QKeySequence other)

int QKeySequence.__getitem__ (self, unsigned long i)

bool QKeySequence.__gt__ (self, QKeySequence other)

int QKeySequence.__int__ (self)

bool QKeySequence.__le__ (self, QKeySequence other)

bool QKeySequence.__lt__ (self, QKeySequence ks)

bool QKeySequence.__ne__ (self, QKeySequence other)


PyQt 4.1 for X11Copyright © Riverbank Computing Ltd and Trolltech AS 2006Qt 4.2.1