2014-09-24 340 views
-1

任何人都可以帮助我如何使用键盘上的任意5个键模拟整个鼠标功能。前四个键用于鼠标光标移动。生成左键单击事件的最后一个键。 光标移动正常工作。但左键单击不适用于旋转框和组合框。这里大写锁定键用于生成左键单击事件。在Qt中模拟鼠标功能

void MainWindow :: keyPressEvent(QKeyEvent *event) 
{ 

    switch(event->key()) 
    { 
    case Qt::Key_CapsLock: 
     QPoint pt(m_pqCursObj->pos().x(),m_pqCursObj->pos().y()); 
     cursor().setPos(pt); 
     QMouseEvent * event1 = new QMouseEvent ((QEvent::MouseButtonPress), QPoint(1,1), 
              Qt::LeftButton, 
              Qt::LeftButton, 
              Qt::NoModifier); 
     QCoreApplication::sendEvent(this,event1); 


     QMouseEvent * event2 = new QMouseEvent ((QEvent::MouseButtonRelease), QPoint(1,1), 
              Qt::LeftButton, 
              Qt::LeftButton, 
              Qt::NoModifier); 

     QObject * ObjunderPos = static_cast<QObject*>(QApplication::widgetAt(QCursor::pos())); 
     QWidget * qWidget = new QWidget; 
     qWidget = (QWidget *)ObjunderPos; 

     if(ObjunderPos) 
     { 
      qDebug()<<"Qobject"; 
      if (qobject_cast<QLineEdit*>(qWidget)) 
       qWidget->setFocus(); 
      QCoreApplication::sendEvent(ObjunderPos,event1); 
      QCoreApplication::sendEvent(ObjunderPos,event2); 
     } 
     break; 
    } 
} 

回答

0

我不确定是否/如何以某种方式使用Qt事件来模拟这种情况,在任何情况下都能正常工作。但是,如果您使用操作系统函数模拟鼠标点击,它将非常容易(在SO中搜索,关于此主题有几个线程)。