Qt 键盘和鼠标组合功能实现
1. 通过过滤器组合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; }
2. 按键与鼠标的结合 键盘按住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; } } }
3. 三键组合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; } }