-1

How to simulate entire mouse functionality using any 5 keys in keyboard? first four keys for mouse cursor movement. last key for generate left click event. cursor movement is properly working. But left click is not working for spin boxes and combo boxes. here caps lock key is using for generate left click event.

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;
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Nagur Basha Shaik
  • 187
  • 1
  • 1
  • 7

1 Answers1

0

I am not sure if/how you can simulate this using Qt events in a way that it works in all cases. However if you simulate the mouse clicks using operating system functions, it will be pretty easy (search in SO, there are several threads about this topic).

Silicomancer
  • 8,604
  • 10
  • 63
  • 130