📄 qkeysequence.cpp
字号:
be considered alternative shortcuts on the same platform for the given \a key.*/QList<QKeySequence> QKeySequence::keyBindings(StandardKey key){ uint platform = QApplicationPrivate::currentPlatform(); QList <QKeySequence> list; for (uint i = 0; i < QKeySequencePrivate::numberOfKeyBindings ; ++i) { QKeyBinding keyBinding = QKeySequencePrivate::keyBindings[i]; if (keyBinding.standardKey == key && (keyBinding.platform & platform)) if (keyBinding.priority > 0) list.prepend(QKeySequence(QKeySequencePrivate::keyBindings[i].shortcut)); else list.append(QKeySequence(QKeySequencePrivate::keyBindings[i].shortcut)); } return list;}/*! Destroys the key sequence. */QKeySequence::~QKeySequence(){ if (!d->ref.deref()) delete d;}/*! \internal KeySequences should never be modified, but rather just created. Internally though we do need to modify to keep pace in event delivery.*/void QKeySequence::setKey(int key, int index){ Q_ASSERT_X(index >= 0 && index < 4, "QKeySequence::setKey", "index out of range"); qAtomicDetach(d); d->key[index] = key;}/*! Returns the number of keys in the key sequence. The maximum is 4. */uint QKeySequence::count() const{ if (!d->key[0]) return 0; if (!d->key[1]) return 1; if (!d->key[2]) return 2; if (!d->key[3]) return 3; return 4;}/*! Returns true if the key sequence is empty; otherwise returns false.*/bool QKeySequence::isEmpty() const{ return !d->key[0];}/*! Returns the shortcut key sequence for the mnemonic in \a text, or an empty key sequence if no mnemonics are found. For example, mnemonic("E&xit") returns \c{Qt::ALT+Qt::Key_X}, mnemonic("&Quit") returns \c{ALT+Key_Q}, and mnemonic("Quit") returns an empty QKeySequence. We provide a \l{accelerators.html}{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.*/QKeySequence QKeySequence::mnemonic(const QString &text){ if(qt_sequence_no_mnemonics) return QKeySequence(); int p = 0; while (p >= 0) { p = text.indexOf(QLatin1Char('&'), p) + 1; if (p <= 0 || p >= (int)text.length()) break; if (text.at(p) != QLatin1Char('&')) { QChar c = text.at(p); if (c.isPrint()) { c = c.toUpper(); return QKeySequence(c.unicode() + Qt::ALT); } } p++; } return QKeySequence();}/*! \fn int QKeySequence::assign(const QString &keys) Adds the given \a keys to the key sequence. \a keys may contain up to four key codes, provided they are separated by a comma; for example, "Alt+X,Ctrl+S,Z". The return value is the number of key codes added.*/int QKeySequence::assign(const QString &ks){ QString keyseq = ks; QString part; int n = 0; int p = 0, diff = 0; // Run through the whole string, but stop // if we have 4 keys before the end. while (keyseq.length() && n < 4) { // We MUST use something to separate each sequence, and space // does not cut it, since some of the key names have space // in them.. (Let's hope no one translate with a comma in it:) p = keyseq.indexOf(QLatin1Char(',')); if (-1 != p) { if (p == keyseq.count() - 1) { // Last comma 'Ctrl+,' p = -1; } else { if (QLatin1Char(',') == keyseq.at(p+1)) // e.g. 'Ctrl+,, Shift+,,' p++; if (QLatin1Char(' ') == keyseq.at(p+1)) { // Space after comma diff = 1; p++; } else { diff = 0; } } } part = keyseq.left(-1 == p ? keyseq.length() : p - diff); keyseq = keyseq.right(-1 == p ? 0 : keyseq.length() - (p + 1)); d->key[n] = decodeString(part); ++n; } return n;}struct QModifKeyName { QModifKeyName() { } QModifKeyName(int q, QChar n) : qt_key(q), name(n) { } QModifKeyName(int q, const QString &n) : qt_key(q), name(n) { } int qt_key; QString name;};Q_GLOBAL_STATIC(QList<QModifKeyName>, globalModifs)Q_GLOBAL_STATIC(QList<QModifKeyName>, globalPortableModifs)/*! Constructs a single key from the string \a str.*/int QKeySequence::decodeString(const QString &str){ return QKeySequencePrivate::decodeString(str, NativeText);}int QKeySequencePrivate::decodeString(const QString &str, QKeySequence::SequenceFormat format){ int ret = 0; QString accel = str.toLower(); bool nativeText = (format == QKeySequence::NativeText); QList<QModifKeyName> *gmodifs; if (nativeText) { gmodifs = globalModifs(); if (gmodifs->isEmpty()) {#ifdef QMAC_CTRL *gmodifs << QModifKeyName(Qt::CTRL, QMAC_CTRL);#endif#ifdef QMAC_ALT *gmodifs << QModifKeyName(Qt::ALT, QMAC_ALT);#endif#ifdef QMAC_META *gmodifs << QModifKeyName(Qt::META, QMAC_META);#endif#ifdef QMAC_SHIFT *gmodifs << QModifKeyName(Qt::SHIFT, QMAC_SHIFT);#endif *gmodifs << QModifKeyName(Qt::CTRL, QLatin1String("ctrl+")) << QModifKeyName(Qt::SHIFT, QLatin1String("shift+")) << QModifKeyName(Qt::ALT, QLatin1String("alt+")) << QModifKeyName(Qt::META, QLatin1String("meta+")); } } else { gmodifs = globalPortableModifs(); if (gmodifs->isEmpty()) { *gmodifs << QModifKeyName(Qt::CTRL, QLatin1String("ctrl+")) << QModifKeyName(Qt::SHIFT, QLatin1String("shift+")) << QModifKeyName(Qt::ALT, QLatin1String("alt+")) << QModifKeyName(Qt::META, QLatin1String("meta+")); } } if (!gmodifs) return ret; QList<QModifKeyName> modifs; if (nativeText) { modifs << QModifKeyName(Qt::CTRL, QShortcut::tr("Ctrl").toLower().append(QLatin1Char('+'))) << QModifKeyName(Qt::SHIFT, QShortcut::tr("Shift").toLower().append(QLatin1Char('+'))) << QModifKeyName(Qt::ALT, QShortcut::tr("Alt").toLower().append(QLatin1Char('+'))) << QModifKeyName(Qt::META, QShortcut::tr("Meta").toLower().append(QLatin1Char('+'))); } modifs += *gmodifs; // Test non-translated ones last QString sl = accel;#ifdef Q_WS_MAC for (int i = 0; i < modifs.size(); ++i) { const QModifKeyName &mkf = modifs.at(i); if (sl.contains(mkf.name)) { ret |= mkf.qt_key; accel.remove(mkf.name); sl = accel; } }#else int i = 0; int lastI = 0; while ((i = sl.indexOf(QLatin1Char('+'), i + 1)) != -1) { const QString sub = sl.mid(lastI, i - lastI + 1); // Just shortcut the check here if we only have one character. // Rational: A modifier will contain the name AND +, so longer than 1, a length of 1 is just // the remaining part of the shortcut (ei. The 'C' in "Ctrl+C"), so no need to check that. if (sub.length() > 1) { for (int j = 0; j < modifs.size(); ++j) { const QModifKeyName &mkf = modifs.at(j); if (sub == mkf.name) { ret |= mkf.qt_key; break; // Shortcut, since if we find an other it would/should just be a dup } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -