【问题标题】:How can I simulate all mouse and keyboard events in Qt that works on Linux and Windows?如何在 Qt 中模拟适用于 Linux 和 Windows 的所有鼠标和键盘事件?
【发布时间】:2014-08-18 16:04:53
【问题描述】:

问题:我有一个设备向我发送一些命令(例如:1,2,3,...),我想根据在 Linux 操作系统和 Windows 操作系统中接收到的命令来模拟鼠标和键盘事件。

我与bool QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority = Qt::NormalEventPriority) 合作,但我不知道接收器传递给它,我对MouseMove 的操作有问题。

我发现这个help 在 linux 操作系统中运行良好,但我在 Windows 操作系统中遇到了这种帮助方法的问题。 有什么方法适用于这两种操作系统?

感谢您的关注。

【问题讨论】:

    标签: c++ linux windows qt


    【解决方案1】:

    您可以通过以下方式设置鼠标位置:

    QCursor::setPos(QPoint(10,10));
    

    鼠标点击模拟也可以通过:

    QMouseEvent * event1 = new QMouseEvent ((QEvent::MouseButtonPress), QPoint(10,10),
        Qt::LeftButton,
        Qt::LeftButton,
        Qt::NoModifier   );
    
    qApp->postEvent((QObject*)myWidget,(QEvent *)event1);
    
    QMouseEvent * event2 = new QMouseEvent ((QEvent::MouseButtonRelease), QPoint(10,10),
        Qt::LeftButton,
        Qt::LeftButton,
        Qt::NoModifier   );
    
    qApp->postEvent((QObject*)myWidget,(QEvent *)event2);
    

    向小部件发送关键事件就像:

    QKeyEvent *event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
    QCoreApplication::postEvent (myWidget, event);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      • 2014-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      • 1970-01-01
      相关资源
      最近更新 更多