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;
// }
}