📄 qkeysequence.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qkeysequence.h"#include "qkeysequence_p.h"#include "private/qapplication_p.h"#ifndef QT_NO_SHORTCUT#include "qshortcut.h"#include "qdebug.h"#ifndef QT_NO_REGEXP# include "qregexp.h"#endif#ifndef QT_NO_DATASTREAM# include "qdatastream.h"#endif#include "qvariant.h"#ifdef Q_WS_MAC# include <private/qt_mac_p.h># define QMAC_CTRL QChar(kCommandUnicode)# define QMAC_META QChar(kControlUnicode)# define QMAC_ALT QChar(kOptionUnicode)# define QMAC_SHIFT QChar(kShiftUnicode)#endif#ifdef Q_WS_MACstatic bool qt_sequence_no_mnemonics = true;#elsestatic bool qt_sequence_no_mnemonics = false;#endifvoid Q_GUI_EXPORT qt_set_sequence_auto_mnemonic(bool b) { qt_sequence_no_mnemonics = !b; }/*! \class QKeySequence \brief The QKeySequence class encapsulates a key sequence as used by shortcuts. \ingroup misc \ingroup shared \mainclass 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: \list \o For standard shortcuts, a \l{QKeySequence::StandardKey}{standard key} can be used to request the platform-specific key sequence associated with each shortcut. \o 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. \o 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. \endlist For example, \gui{Ctrl P} might be a sequence used as a shortcut for printing a document, and can be specified in any of the following ways: \code QKeySequence(QKeySequence::Print} QKeySequence(tr("Ctrl+P")) QKeySequence(tr("Ctrl+p")) QKeySequence(Qt::CTRL + Qt::Key_P) \endcode 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 \key{Shift} key to activate a shortcut specified with "Ctrl+P". However, for other keys, the use of \key{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 \l{#Keyboard Layout Issues}{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. \bold{Note:} On Mac OS X, references to "Ctrl", Qt::CTRL, Qt::Control and Qt::ControlModifier correspond to the \key Command keys on the Macintosh keyboard, and references to "Meta", Qt::META, Qt::Meta and Qt::MetaModifier correspond to the \key 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. \section1 Standard Shortcuts QKeySequence defines many \l{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 \key Ctrl value corresponds to the \key Command keys on the Macintosh keyboard, and the \key Meta value corresponds to the \key Control keys. \table \header \i StandardKey \i Windows \i Mac OS X \i KDE \i GNOME \row \i HelpContents \i F1 \i Ctrl+? \i F1 \i F1 \row \i WhatsThis \i Shift+F1 \i Shift+F1 \i Shift+F1 \i Shift+F1 \row \i Open \i Ctrl+O \i Ctrl+O \i Ctrl+O \i Ctrl+O \row \i Close \i Ctrl+F4, Ctrl+W \i Ctrl+W, Ctrl+F4 \i Ctrl+W \i Ctrl+W \row \i Save \i Ctrl+S \i Ctrl+S \i Ctrl+S \i Ctrl+S \row \i New \i Ctrl+N \i Ctrl+N \i Ctrl+N \i Ctrl+N \row \i Delete \i Del \i Del, Meta+D \i Del, Ctrl+D \i Del, Ctrl+D \row \i Cut \i Ctrl+X, Shift+Del \i Ctrl+X \i Ctrl+X, F20, Shift+Del \i Ctrl+X, F20, Shift+Del \row \i Copy \i Ctrl+C, Ctrl+Ins \i Ctrl+C \i Ctrl+C, F16, Ctrl+Ins \i Ctrl+C, F16, Ctrl+Ins \row \i Paste \i Ctrl+V, Shift+Ins \i Ctrl+V \i Ctrl+V, F18, Shift+Ins \i Ctrl+V, F18, Shift+Ins \row \i Undo \i Ctrl+Z, Alt+Backspace \i Ctrl+Z \i Ctrl+Z, F14 \i Ctrl+Z, F14 \row \i Redo \i Ctrl+Y, Shift+Ctrl+Z, Alt+Shift+Backspace \i Ctrl+Shift+Z, Ctrl+Y \i Ctrl+Shift+Z \i Ctrl+Shift+Z \row \i Back \i Alt+Left, Backspace \i Ctrl+[ \i Alt+Left \i Alt+Left \row \i Forward \i Alt+Right, Shift+Backspace \i Ctrl+] \i Alt+Right \i Alt+Right \row \i Refresh \i F5 \i F5 \i F5 \i Ctrl+R, F5 \row \i ZoomIn \i Ctrl+Plus \i Ctrl+Plus \i Ctrl+Plus \i Ctrl+Plus \row \i ZoomOut \i Ctrl+Minus \i Ctrl+Minus \i Ctrl+Minus \i Ctrl+Minus \row \i Print \i Ctrl+P \i Ctrl+P \i Ctrl+P \i Ctrl+P \row \i AddTab \i Ctrl+T \i Ctrl+T \i Ctrl+Shift+N, Ctrl+T \i Ctrl+T \row \i NextChild \i Ctrl+Tab, Forward, Ctrl+F6 \i Ctrl+}, Forward, Ctrl+Tab \i Ctrl+Tab, Forward, Ctrl+Comma \i Ctrl+Tab, Forward \row \i PreviousChild \i Ctrl+Shift+Tab, Back, Ctrl+Shift+F6 \i Ctrl+{, Back, Ctrl+Shift+Tab \i Ctrl+Shift+Tab, Back, Ctrl+Period \i Ctrl+Shift+Tab, Back \row \i Find \i Ctrl+F \i Ctrl+F \i Ctrl+F \i Ctrl+F \row \i FindNext \i F3, Ctrl+G \i Ctrl+G \i F3 \i Ctrl+G, F3 \row \i FindPrevious \i Shift+F3, Ctrl+Shift+G \i Ctrl+Shift+G \i Shift+F3 \i Ctrl+Shift+G, F3 \row \i Replace \i Ctrl+H \i (none) \i Ctrl+R \i Ctrl+H \row \i SelectAll \i Ctrl+A \i Ctrl+A \i Ctrl+A \i Ctrl+A \row \i Bold \i Ctrl+B \i Ctrl+B \i Ctrl+B \i Ctrl+B \row \i Italic \i Ctrl+I \i Ctrl+I \i Ctrl+I \i Ctrl+I \row \i Underline \i Ctrl+U \i Ctrl+U \i Ctrl+U \i Ctrl+U \row \i MoveToNextChar \i Right \i Right \i Right \i Right \row \i MoveToPreviousChar \i Left \i Left \i Left \i Left \row \i MoveToNextWord \i Ctrl+Right \i Alt+Right \i Ctrl+Right \i Ctrl+Right \row \i MoveToPreviousWord \i Ctrl+Left \i Alt+Left \i Ctrl+Left \i Ctrl+Left \row \i MoveToNextLine \i Down \i Down \i Down \i Down \row \i MoveToPreviousLine \i Up \i Up \i Up \i Up \row \i MoveToNextPage \i PgDown \i PgDown, Alt+PgDown, Meta+Down, Meta+PgDown\i PgDown \i PgDown \row \i MoveToPreviousPage \i PgUp \i PgUp, Alt+PgUp, Meta+Up, Meta+PgUp \i PgUp \i PgUp \row \i MoveToStartOfLine \i Home \i Ctrl+Left, Meta+Left \i Home \i Home \row \i MoveToEndOfLine \i End \i Ctrl+Right, Meta+Right \i End \i End \row \i MoveToStartOfBlock \i (none) \i Alt+Up, Meta+A \i (none) \i (none) \row \i MoveToEndOfBlock \i (none) \i Alt+Down, Meta+E \i (none) \i (none) \row \i MoveToStartOfDocument\i Ctrl+Home \i Ctrl+Up, Home \i Ctrl+Home \i Ctrl+Home \row \i MoveToEndOfDocument \i Ctrl+End \i Ctrl+Down, End \i Ctrl+End \i Ctrl+End \row \i SelectNextChar \i Shift+Right \i Shift+Right \i Shift+Right \i Shift+Right \row \i SelectPreviousChar \i Shift+Left \i Shift+Left \i Shift+Left \i Shift?left \row \i SelectNextWord \i Ctrl+Shift+Right \i Alt+Shift+Right \i Ctrl+Shift+Right \i Ctrl+Shift+Right \row \i SelectPreviousWord \i Ctrl+Shift+Left \i Alt+Shift+Left \i Ctrl+Shift+Left \i Ctrl+Shift+Left \row \i SelectNextLine \i Shift+Down \i Shift+Down \i Shift+Down \i Shift+Down \row \i SelectPreviousLine \i Shift+Up \i Shift+Up \i Shift+Up \i Shift+Up \row \i SelectNextPage \i Shift+PgDown \i Shift+PgDown \i Shift+PgDown \i Shift+PgDown \row \i SelectPreviousPage \i Shift+PgUp \i Shift+PgUp \i Shift+PgUp \i Shift+PgUp \row \i SelectStartOfLine \i Shift+Home \i Ctrl+Shift+Left \i Shift+Home \i Shift+Home \row \i SelectEndOfLine \i Shift+End \i Ctrl+Shift+Right \i Shift+End \i Shift+End \row \i SelectStartOfBlock \i (none) \i Alt+Shift+Up \i (none) \i (none) \row \i SelectEndOfBlock \i (none) \i Alt+Shift+Down \i (none) \i (none) \row \i SelectStartOfDocument\i Ctrl+Shift+Home \i Ctrl+Shift+Up, Shift+Home \i Ctrl+Shift+Home\i Ctrl+Shift+Home \row \i SelectEndOfDocument \i Ctrl+Shift+End \i Ctrl+Shift+Down, Shift+End \i Ctrl+Shift+End \i Ctrl+Shift+End \row \i DeleteStartOfWord \i Ctrl+Backspace \i Alt+Backspace \i Ctrl+Backspace \i Ctrl+Backspace \row \i DeleteEndOfWord \i Ctrl+Del \i (none) \i Ctrl+Del \i Ctrl+Del \row \i DeleteEndOfLine \i (none) \i (none) \i Ctrl+K \i Ctrl+K \endtable 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. \section1 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 \key{Ctrl S} ("Ctrl+S") or \key{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, \key{Ctrl plus} and \key{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 \key{+} and \key{-} keys are not adjacent on the keyboard, but will still be able to activate both shortcuts without needing to press the \key{Shift} key. However, users with British keyboards will need to hold down the \key{Shift} key to enter the \key{+} symbol, making the shortcut effectively the same as "Ctrl+Shift+=".
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -