qt5--鍵盤事件


需要 #include<QKeyEvent>

常用操作

win.h

#ifndef WIN_H
#define WIN_H

#include <QWidget>
#include <QDebug>
#include<QKeyEvent>  //鍵盤模塊

class Win : public QWidget
{
    Q_OBJECT

public:
    Win(QWidget *parent = nullptr);
    ~Win();

    void keyPressEvent(QKeyEvent *ev); //鍵盤按下事件

};
#endif // WIN_H

win.cpp

#include "win.h"

Win::Win(QWidget *parent)
    : QWidget(parent)
{
}

Win::~Win()
{
}

void Win::keyPressEvent(QKeyEvent *e)
{
int key=e->key();  //返回觸發事件鍵的鍵值
//大寫字母的ASCii嗎
//不加shift鍵的ASCii嗎

static int i=0;
//一個普通鍵
if(e->key() == Qt::Key_F5){  //如果按下的是F5鍵
    //按鍵詳情:https://blog.csdn.net/weixin_42692504/article/details/95116279
    qDebug()<<"F5:"<<i;
}

//兩鍵組合
if(e->modifiers() == Qt::ControlModifier){ //如果按下了CTRL鍵
       if(e->key() == Qt::Key_M){
           qDebug()<<"CTRL+m:"<<i;
       }
    }

if(e->modifiers() == Qt::AltModifier){ //如果按下了ALT鍵
       if(e->key() == Qt::Key_M){
           qDebug()<<"ALT+m:"<<i;
       }
    }

if(e->modifiers() == Qt::ShiftModifier){ //如果按下了Shift鍵
       if(e->key() == Qt::Key_M){
           qDebug()<<"Shift+m:"<<i;
       }
    }

//三鍵組合Shift + Ctrl + A的實現
if (e->modifiers() == (Qt::ShiftModifier | Qt::ControlModifier) && e->key() == Qt::Key_A)
      {
          qDebug()<<"CTRL+Shift+a:"<<i;
      }



//qDebug()<<key;
i++;
QWidget::keyPressEvent(e);   //保存默認事件
}

main.cpp

#include "win.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Win w;
    w.show();
    return a.exec();
}

以上工程下載地址:鏈接:https://pan.baidu.com/s/1g6nUHEoGVhDQC7dLrJlLtg    提取碼:6666   

 

 

按鍵與自動重復

自動重復是指按下鍵盤上的鍵(修飾鍵除外)不放時,會不斷重復的發送鍵按下事件,Qt 默認是啟用自動重復的,若要實現類似按鍵 A+D 之類的快捷鍵,就需要關閉自動重復。可使用如下方法來關閉自動重復
if( QKeyEvent::isAutoRepeat() ) return; // 若自動重復則什么也不做
 

Qt的鍵盤事件整體表現為,按住一個鍵時:
1、第一次觸發keyPressEvent(), isAutoRepeat()返回false

2、沒有觸發keyReleaseEvent(),停頓一會

3、再一次觸發keyPressEvent(),isAutoRepeat()返回true

4、觸發keyReleaseEvent()

5、若沒松開按鍵,isAutoRepeat()返回true,返回第3步;若松開按鍵,isAutoRepeat()返回false

 

實例

win.h

#ifndef WIN_H
#define WIN_H

#include <QWidget>
#include <QDebug>
#include <QKeyEvent>

class Win : public QWidget
{
    Q_OBJECT

public:
    Win(QWidget *parent = nullptr);
    ~Win();

    void keyPressEvent(QKeyEvent *e); //鍵盤按下事件
    void keyReleaseEvent(QKeyEvent *e); //鍵盤釋放事件

};
#endif // WIN_H

win.cpp

#include "win.h"

Win::Win(QWidget *parent)
    : QWidget(parent)
{
}

Win::~Win()
{
}

void Win::keyPressEvent(QKeyEvent *e)
{
    static int i=0;
    if (e->key() == Qt::Key_Up){
        //避免自動重復,按鍵重復時return;
        if (e->isAutoRepeat()){return;}
        //isAutoRepeat()  返回是不是重復按住的事件
    }

    qDebug()<<"鍵盤按下事件:"<<i;
    i++;
}

void Win::keyReleaseEvent(QKeyEvent *e)
{
    static int ii=0;
    if (e->key() == Qt::Key_Up){
        //避免自動重復,按鍵重復時return;
        if (e->isAutoRepeat()){return;}
        //isAutoRepeat()  返回是不是重復按住的事件
    }

    qDebug()<<"鍵盤釋放事件:"<<ii;
    ii++;
}

main.cpp

#include "win.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Win w;
    w.show();
    return a.exec();
}

上面工程下載地址:鏈接:https://pan.baidu.com/s/17XlqMD5o4J5bCVkTTNMhzw   提取碼:6666  

 

鍵盤捕獲

mybutton.h

#ifndef MYBUTTON_H
#define MYBUTTON_H

#include <QPushButton>
#include <QDebug>
#include <QKeyEvent>

class mybutton : public QPushButton
{
public:
    mybutton();

    void keyPressEvent(QKeyEvent *e);
};

#endif // MYBUTTON_H

mybutton.cpp

#include "mybutton.h"

mybutton::mybutton()
{

}

void mybutton::keyPressEvent(QKeyEvent *e)
{
 static int i=0;
 qDebug()<<"button鍵盤按鍵事件:"<<this->objectName()<<i;
 QWidget* ww= keyboardGrabber();//返回正在捕獲鍵盤輸入的部件,若沒有則返回 0
 //QPushButton(0x33fcba8)
 qDebug()<<"正在捕獲的控件:"<<ww;
 if(e->key() == Qt::Key_Q){  //如果按下的是Q鍵
     qDebug()<<"按下了Q鍵";
     this->releaseKeyboard();   //釋放捕獲的鍵盤輸入
  }
i++;
}

win.h

#ifndef WIN_H
#define WIN_H

#include <QWidget>
#include "mybutton.h"
#include <QDebug>

class Win : public QWidget
{
    Q_OBJECT

public:
    Win(QWidget *parent = nullptr);
    ~Win();

    void keyPressEvent(QKeyEvent *e);
private:
    mybutton* button;
    mybutton* button1;

};
#endif // WIN_H

win.cpp

#include "win.h"

Win::Win(QWidget *parent)
    : QWidget(parent)
{
    this->resize(300,400);
    button=new mybutton();
    button->setParent(this);
    button->setText("AAA");
    button->move(10,10);
    button->resize(100,100);
    button->setObjectName("aaa");

    button1=new mybutton();
    button1->setParent(this);
    button1->setText("BBB");
    button1->move(150,10);
    button1->resize(100,100);
    button1->setObjectName("bbb");

    button->grabKeyboard();//指定控件捕獲鍵盤
    /*使按鈕 AAA 捕獲鍵盤,此時產生的鍵盤事件都只會發送給按鈕 AAA,也就是說
        其他部件無法獲得鍵盤事件。
        只有可見的部件才能捕獲鍵盤輸入,若 isVisible()返回 false,則該部件不能調用grabKeyboard()函數
*/
}

Win::~Win()
{
}

void Win::keyPressEvent(QKeyEvent *e)
{
qDebug()<<"win鍵盤事件";
}

main.cpp

#include "win.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Win w;
    w.show();
    return a.exec();
}

上面工程下載地址:鏈接:https://pan.baidu.com/s/1S4NTFumnZFtHCW9YJ9i3-w     提取碼:6666  

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM