原创不易,转载请注明原出处: 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); }};以上信息均来自网络公开信息、开源代码等共享渠道,若有侵权,请告知撤销。
新闻热点
疑难解答