原創不易,轉載請注明原出處: http://write.blog.csdn.net/mdeditor#!postId=56009783
QKeyEvent, QKeyEventTransition用于存儲捕捉到的按鍵事件, 包含單個按鍵,組合按鍵,按鍵序列(設置Qt::WidgetAttribute–Qt::WA_KeyCompression后可獲得) 包括keyPressEvent, keyReleaseEvent 可以通過重載兩者來自定義按鍵事件的響應,不處理的按鍵事件應繼續交給父類函數處理:QWidget::keyPressEvent(event); //保存默認事件
Qt中的按鍵分兩種:auto-repeating key、initial key。 注:keyPressEvent與keyReleaseEvent行為類似,不重復贅述
auto-repeating key基本是普通的字母、數字按鍵。長按會不斷產生keyPressEvent。處理時需要屏蔽重復的事件:“if(event->isAutoRepeat()) return;
initial key類似Ctrl, Shift, Alt等輔助類按鍵,只在按下時產生一個keyPressEvent。
Qt中與按鍵相關類有QKeySequence, QKeySequenceEdit QKeySequence用于存儲按鍵的組合,最多4個按鍵。 1、包含3類按鍵值QKeySequence:StandardKey,Qt::Key, Qt::Modifier 2、有3種方式:標準快捷鍵(系統定義),自定義快捷鍵(覆蓋系統定義),硬編碼快捷鍵(自由組合) 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.
QShortcut自動監聽keyEvent相關事件,檢測指定的QKeySequence,執行指定函數。
雙擊Ctrl不符合qt的快捷鍵規則,只能通過重載QKeyEvent的方式自定義按鍵事件響應。使用QTimer定時器來限制雙擊Ctrl的時間間隔。
class control : public QWidget{ Q_OBJECT QTimer *m_shortcutTimer;protected: void keyPressEvent(QKeyEvent *ke) { if(ke->key() == Qt::Key_Control) { if(m_shortcutTimer->isActive()) QMessageBox::aboutQt(this); else m_shortcutTimer->start(); } else QWidget::keyPressEvent(ke); }};以上信息均來自網絡公開信息、開源代碼等共享渠道,若有侵權,請告知撤銷。
新聞熱點
疑難解答