QKeyEvent::isAutoRepeat()返回的是個布爾型的值, 函數
新建項目:KeyLongPress 測試
main.cpp就很少說了... ui
keyLongPress.ui是默認的,什麼都沒改。 this
//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的, spa
長按看下輸出:是Press和Release一直在不停的輸出的。 code
示意圖: 事件