Qt Jambi Home

com.trolltech.qt.gui
Class QKeySequence

java.lang.Object
  extended by com.trolltech.qt.QSignalEmitter
      extended by com.trolltech.qt.QtJambiObject
          extended by com.trolltech.qt.gui.QKeySequence
All Implemented Interfaces:
QtJambiInterface, java.lang.Comparable

public class QKeySequence
extends QtJambiObject
implements java.lang.Comparable

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

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 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 Mac OS X, 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 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.

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.

StandardKeyWindowsMac OS XKDEGNOME
HelpContentsF1Ctrl+?F1F1
WhatsThisShift+F1Shift+F1Shift+F1Shift+F1
OpenCtrl+OCtrl+OCtrl+OCtrl+O
CloseCtrl+F4, Ctrl+WCtrl+W, Ctrl+F4Ctrl+WCtrl+W
SaveCtrl+SCtrl+SCtrl+SCtrl+S
NewCtrl+NCtrl+NCtrl+NCtrl+N
DeleteDelDel, Meta+DDel, Ctrl+DDel, Ctrl+D
CutCtrl+X, Shift+DelCtrl+XCtrl+X, F20, Shift+DelCtrl+X, F20, Shift+Del
CopyCtrl+C, Ctrl+InsCtrl+CCtrl+C, F16, Ctrl+InsCtrl+C, F16, Ctrl+Ins
PasteCtrl+V, Shift+InsCtrl+VCtrl+V, F18, Shift+InsCtrl+V, F18, Shift+Ins
UndoCtrl+Z, Alt+BackspaceCtrl+ZCtrl+Z, F14Ctrl+Z, F14
RedoCtrl+Y, Shift+Ctrl+Z, Alt+Shift+BackspaceCtrl+Shift+Z, Ctrl+YCtrl+Shift+ZCtrl+Shift+Z
BackAlt+Left, BackspaceCtrl+[Alt+LeftAlt+Left
ForwardAlt+Right, Shift+BackspaceCtrl+]Alt+RightAlt+Right
RefreshF5F5F5Ctrl+R, F5
ZoomInCtrl+PlusCtrl+PlusCtrl+PlusCtrl+Plus
ZoomOutCtrl+MinusCtrl+MinusCtrl+MinusCtrl+Minus
PrintCtrl+PCtrl+PCtrl+PCtrl+P
AddTabCtrl+TCtrl+TCtrl+Shift+N, Ctrl+TCtrl+T
NextChildCtrl+Tab, Forward, Ctrl+F6Ctrl+}, Forward, Ctrl+TabCtrl+Tab, Forward, Ctrl+CommaCtrl+Tab, Forward
PreviousChildCtrl+Shift+Tab, Back, Ctrl+Shift+F6Ctrl+{, Back, Ctrl+Shift+TabCtrl+Shift+Tab, Back, Ctrl+PeriodCtrl+Shift+Tab, Back
FindCtrl+FCtrl+FCtrl+FCtrl+F
FindNextF3, Ctrl+GCtrl+GF3Ctrl+G, F3
FindPreviousShift+F3, Ctrl+Shift+GCtrl+Shift+GShift+F3Ctrl+Shift+G, F3
ReplaceCtrl+H(none)Ctrl+RCtrl+H
SelectAllCtrl+ACtrl+ACtrl+ACtrl+A
BoldCtrl+BCtrl+BCtrl+BCtrl+B
ItalicCtrl+ICtrl+ICtrl+ICtrl+I
UnderlineCtrl+UCtrl+UCtrl+UCtrl+U
MoveToNextCharRightRightRightRight
MoveToPreviousCharLeftLeftLeftLeft
MoveToNextWordCtrl+RightAlt+RightCtrl+RightCtrl+Right
MoveToPreviousWordCtrl+LeftAlt+LeftCtrl+LeftCtrl+Left
MoveToNextLineDownDownDownDown
MoveToPreviousLineUpUpUpUp
MoveToNextPagePgDownPgDown, Alt+PgDown, Meta+Down, Meta+PgDownPgDownPgDown
MoveToPreviousPagePgUpPgUp, Alt+PgUp, Meta+Up, Meta+PgUpPgUpPgUp
MoveToStartOfLineHomeCtrl+Left, Meta+LeftHomeHome
MoveToEndOfLineEndCtrl+Right, Meta+RightEndEnd
MoveToStartOfBlock(none)Alt+Up, Meta+A(none)(none)
MoveToEndOfBlock(none)Alt+Down, Meta+E(none)(none)
MoveToStartOfDocumentCtrl+HomeCtrl+Up, HomeCtrl+HomeCtrl+Home
MoveToEndOfDocumentCtrl+EndCtrl+Down, EndCtrl+EndCtrl+End
SelectNextCharShift+RightShift+RightShift+RightShift+Right
SelectPreviousCharShift+LeftShift+LeftShift+LeftShift?left
SelectNextWordCtrl+Shift+RightAlt+Shift+RightCtrl+Shift+RightCtrl+Shift+Right
SelectPreviousWordCtrl+Shift+LeftAlt+Shift+LeftCtrl+Shift+LeftCtrl+Shift+Left
SelectNextLineShift+DownShift+DownShift+DownShift+Down
SelectPreviousLineShift+UpShift+UpShift+UpShift+Up
SelectNextPageShift+PgDownShift+PgDownShift+PgDownShift+PgDown
SelectPreviousPageShift+PgUpShift+PgUpShift+PgUpShift+PgUp
SelectStartOfLineShift+HomeCtrl+Shift+LeftShift+HomeShift+Home
SelectEndOfLineShift+EndCtrl+Shift+RightShift+EndShift+End
SelectStartOfBlock(none)Alt+Shift+Up(none)(none)
SelectEndOfBlock(none)Alt+Shift+Down(none)(none)
SelectStartOfDocumentCtrl+Shift+HomeCtrl+Shift+Up, Shift+HomeCtrl+Shift+HomeCtrl+Shift+Home
SelectEndOfDocumentCtrl+Shift+EndCtrl+Shift+Down, Shift+EndCtrl+Shift+EndCtrl+Shift+End
DeleteStartOfWordCtrl+BackspaceAlt+BackspaceCtrl+BackspaceCtrl+Backspace
DeleteEndOfWordCtrl+Del(none)Ctrl+DelCtrl+Del
DeleteEndOfLine(none)(none)Ctrl+KCtrl+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.

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)

See Also:
QShortcut

Nested Class Summary
static class QKeySequence.SequenceFormat
          Press link for info on QKeySequence.SequenceFormat
static class QKeySequence.SequenceMatch
          Press link for info on QKeySequence.SequenceMatch
static class QKeySequence.StandardKey
          This enum represent standard key bindings.
 
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
QSignalEmitter.Signal0, QSignalEmitter.Signal1<A>, QSignalEmitter.Signal2<A,B>, QSignalEmitter.Signal3<A,B,C>, QSignalEmitter.Signal4<A,B,C,D>, QSignalEmitter.Signal5<A,B,C,D,E>, QSignalEmitter.Signal6<A,B,C,D,E,F>, QSignalEmitter.Signal7<A,B,C,D,E,F,G>, QSignalEmitter.Signal8<A,B,C,D,E,F,G,H>, QSignalEmitter.Signal9<A,B,C,D,E,F,G,H,I>
 
Constructor Summary
QKeySequence()
          Constructs an empty key sequence.
QKeySequence(int k1)
          Equivalent to QKeySequence(k1, 0, 0, 0).
QKeySequence(int k1, int k2)
          Equivalent to QKeySequence(k1, k2, 0, 0).
QKeySequence(int k1, int k2, int k3)
          Equivalent to QKeySequence(k1, k2, k3, 0).
QKeySequence(int k1, int k2, int k3, int k4)
          Constructs a key sequence with up to 4 keys k1, k2, k3 and k4.
QKeySequence(QKeySequence.StandardKey key)
          Constructs a QKeySequence object for the given key.
QKeySequence(QKeySequence ks)
          Copy constructor.
QKeySequence(java.lang.String key)
          Creates a key sequence from the key string.
 
Method Summary
 int at(int i)
          Returns the key at index i in this key sequence.
 int compareTo(java.lang.Object other)
          
 int count()
          Returns the number of keys in the key sequence.
 boolean equals(java.lang.Object other)
          
static QKeySequence fromNativePointer(QNativePointer nativePointer)
          This function returns the QKeySequence instance pointed to by nativePointer
static QKeySequence fromString(java.lang.String str)
          Equivalent to fromString(str, PortableText).
static QKeySequence fromString(java.lang.String str, QKeySequence.SequenceFormat format)
          Return a QKeySequence from the string str based on format.
 boolean isEmpty()
          Returns true if the key sequence is empty; otherwise returns false.
static java.util.List<QKeySequence> keyBindings(QKeySequence.StandardKey key)
          Returns a list of key bindings for the given key.
 QKeySequence.SequenceMatch matches(QKeySequence seq)
          Matches the sequence with seq.
static QKeySequence mnemonic(java.lang.String text)
          Returns the shortcut key sequence for the mnemonic in text, or an empty key sequence if no mnemonics are found.
static QNativePointer nativePointerArray(QKeySequence[] array)
          This function returns a QNativePointer that is pointing to the specified QKeySequence array.
 void readFrom(QDataStream out)
          Reads a QKeySequence from out.
 int toInt()
          For backward compatibility.
 java.lang.String toString()
          Equivalent to toString(PortableText).
 java.lang.String toString(QKeySequence.SequenceFormat format)
          Return a string representation of the key sequence, based on format.
 void writeTo(QDataStream in)
          Writes thisQKeySequence to in.
 
Methods inherited from class com.trolltech.qt.QtJambiObject
dispose, disposed, finalize, reassignNativeResources, tr, tr, tr
 
Methods inherited from class com.trolltech.qt.QSignalEmitter
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread
 
Methods inherited from class java.lang.Object
clone, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.trolltech.qt.QtJambiInterface
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership
 

Constructor Detail

QKeySequence

public QKeySequence(int k1,
                    int k2,
                    int k3)

Equivalent to QKeySequence(k1, k2, k3, 0).


QKeySequence

public QKeySequence(int k1,
                    int k2)

Equivalent to QKeySequence(k1, k2, 0, 0).


QKeySequence

public QKeySequence(int k1)

Equivalent to QKeySequence(k1, 0, 0, 0).


QKeySequence

public QKeySequence(int k1,
                    int k2,
                    int k3,
                    int k4)

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

public QKeySequence(QKeySequence.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.


QKeySequence

public QKeySequence(QKeySequence ks)

Copy constructor. Makes a copy of ks.


QKeySequence

public QKeySequence()

Constructs an empty key sequence.


QKeySequence

public QKeySequence(java.lang.String 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.

Method Detail

count

public final int count()

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


isEmpty

public final boolean isEmpty()

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


matches

public final QKeySequence.SequenceMatch matches(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.


writeTo

public final void writeTo(QDataStream in)
Writes thisQKeySequence to in.


readFrom

public final void readFrom(QDataStream out)
Reads a QKeySequence from out.


toString

public final java.lang.String toString()

Equivalent to toString(PortableText).

Overrides:
toString in class java.lang.Object

toString

public final java.lang.String toString(QKeySequence.SequenceFormat format)

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 menu bar.

See Also:
fromString

fromString

public static QKeySequence fromString(java.lang.String str)

Equivalent to fromString(str, PortableText).


fromString

public static QKeySequence fromString(java.lang.String str,
                                      QKeySequence.SequenceFormat format)

Return a QKeySequence from the string str based on format.

See Also:
toString

keyBindings

public static java.util.List<QKeySequence> keyBindings(QKeySequence.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.


mnemonic

public static QKeySequence mnemonic(java.lang.String 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.


fromNativePointer

public static QKeySequence fromNativePointer(QNativePointer nativePointer)
This function returns the QKeySequence instance pointed to by nativePointer

Parameters:
nativePointer - the QNativePointer of which object should be returned.

nativePointerArray

public static QNativePointer nativePointerArray(QKeySequence[] array)
This function returns a QNativePointer that is pointing to the specified QKeySequence array.

Parameters:
array - the array that the returned pointer will point to.
Returns:
a QNativePointer that is pointing to the specified array.

equals

public boolean equals(java.lang.Object other)

Overrides:
equals in class java.lang.Object

compareTo

public int compareTo(java.lang.Object other)

Specified by:
compareTo in interface java.lang.Comparable

toInt

public final int toInt()
For backward compatibility. returns the first keycode as integer. If the key sequence is empty, 0 is returned.


at

public final int at(int i)
Returns the key at index i in this key sequence.


Qt Jambi Home