简体   繁体   English

如何在 Qt 中模拟适用于 Linux 和 Windows 的所有鼠标和键盘事件?

[英]How can I simulate all mouse and keyboard events in Qt that works on Linux and Windows?

Problem: I have a device that send to me some commands (eg: 1,2,3,...), i want simulate mouse and keyboard events according to received commands in Both Linux OS and Windows OS.问题:我有一个设备向我发送一些命令(例如:1,2,3,...),我想根据 Linux 操作系统和 Windows 操作系统中收到的命令模拟鼠标和键盘事件。

I worked with bool QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority = Qt::NormalEventPriority) but i don't know what receiver pass to it and i has problem with MouseMove action with it.我与bool QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority = Qt::NormalEventPriority)但我不知道什么接收器传递给它,并且我对MouseMove操作有问题。

I found this help that works good in linux OS, but i have problem with this help approach in Windows OS.我发现这个帮助在 linux 操作系统中效果很好,但我在 Windows 操作系统中遇到了这种帮助方法的问题。 Is there any approach that works in both OS?是否有任何方法适用于两种操作系统?

Thanks for your attention.感谢您的关注。

You can set mouse position by :您可以通过以下方式设置鼠标位置:

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

Also mouse click simulation could be done by :鼠标点击模拟也可以通过以下方式完成:

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);

Sending a key event to a widget is like :将关键事件发送到小部件就像:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM