Qt 獲取組合鍵 鍵盤按住某鍵 鼠標組合實現


#include "mainwindow.h"

#include <QDebug>
#include <QKeyEvent>
#include <QMouseEvent>
 
        
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    this->setupUi(this);
    QWidget::installEventFilter(this);
}
 
        
MainWindow::~MainWindow()
{
 
        
}
 
        
//通過過濾器組合Control + Enter 按鍵
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if(event->type()==event->KeyPress)
    {
        QKeyEvent *keyEvent = (QKeyEvent *)event;
        if(keyEvent->key() == Qt::Key_Return && (keyEvent->modifiers() & Qt::ControlModifier))
        {
            qDebug()<<"EnterKey + ControlKey";
            return true;
        }
    }
    return false;
}
 
        
//按鍵與鼠標的結合 鍵盤按住Ctrl鍵 + 鼠標左鍵的實現
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    // 獲取鼠標在點擊窗體上的坐標
    QPoint pos = event->pos();
    qDebug()<<pos;
    if(QApplication::keyboardModifiers() == Qt::ShiftModifier)
    {
        if(event->button() == Qt::LeftButton)
        {
            qDebug()<<"ShiftKey + MOuseLeftButton";
            return;
        }
    }
 
        
    if(QApplication::keyboardModifiers() == Qt::ControlModifier)
    {
        if(event->button() == Qt::RightButton)
        {
            qDebug()<<"ShiftKey + MOuseRightButton";
            return;
        }
    }
}
 
        
//三鍵組合Shift + Ctrl + A的實現
void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if (event->modifiers() == (Qt::ShiftModifier | Qt::ControlModifier) && event->key() == Qt::Key_A)
    {
        qDebug()<<"ShiftKey + controlKey + A";
        return;
    }
 
        
//    if (event->modifiers() == (Qt::ShiftModifier | Qt::ControlModifier))
//    {
//        qDebug()<<"ShiftKey + controlKey";
//        return;
//    }
}
 
        
 
       


免責聲明!

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



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