标签:
QKeyEvent::isAutoRepeat()返回的是个布尔型的值,
新建项目:KeyLongPress
main.cpp就不多说了...
keyLongPress.ui是默认的,什么都没改。
//keyLongPress.h #ifndef KEYLONGPRESS_H #define KEYLONGPRESS_H #include <QDebug> //别忘了我 #include <QWidget> #include <QKeyEvent> //也别忘了我 namespace Ui { class KeyLongPress; } class KeyLongPress : public QWidget { Q_OBJECT public: explicit KeyLongPress(QWidget *parent = 0); ~KeyLongPress(); protected: void keyPressEvent(QKeyEvent *event); //按键按下事件 void keyReleaseEvent(QKeyEvent *event); //按键释放事件 private: Ui::KeyLongPress *ui; }; #endif // KEYLONGPRESS_H...
//keyLongPress.cpp #include "keyLongPress.h" #include "ui_keyLongPress.h" KeyLongPress::KeyLongPress(QWidget *parent) : QWidget(parent), ui(new Ui::KeyLongPress) { ui->setupUi(this); } KeyLongPress::~KeyLongPress() { delete ui; } //QMouseEvent没有isAutoRepeat()方法...无语了, //这样有些操作就不方便实现了, //比如长按鼠标多少秒呼出某个(登录)窗口啥的。 //! [按键按下事件-Begin] void KeyLongPress::keyPressEvent(QKeyEvent *event) { if(event->isAutoRepeat()){ qDebug() << "按键长按中..."; } else{ qDebug() << "按键按下"; } } //! [按键按下事件-End] //! [按键释放事件-Begin] void KeyLongPress::keyReleaseEvent(QKeyEvent *event) { if(event->isAutoRepeat()){ qDebug() << "按键不断释放..."; } else{ qDebug() << "按键释放"; } } //! [按键释放事件-End]
你Click看下输出:是只有1次Press和1次Release的,
长按看下输出:是Press和Release一直在不停的输出的。
示意图:
标签:
原文地址:http://my.oschina.net/jannn/blog/515291