Qt 在 mac OSX 上,单击桌面

Qt on mac OSX, clicking the desktop

本文关键字:单击 桌面 OSX mac Qt      更新时间:2023-10-16

我正在Mac OSX 10上试验Qt。我想做什么:将光标移动到特定位置,然后左键单击桌面项目(在本例中为桌面左上角的"Apple"符号(。我已经尝试过QT测试事件:

首先是将"mouseClick"添加到QTestEventList并模拟它:

list.addMouseClick(Qt::LeftButton, Qt::NoModifier, point);
QWidget* widget = QApplication::desktop();
list.simulate(widget);

这没有成功。我也试过:

QPoint point(26,11);
QCursor::setPos(point);
list.addMouseClick(Qt::LeftButton, Qt::NoModifier);
QWidget* widget = QApplication::desktop();
list.simulate(widget);

这也不起作用。我的最后一次尝试:

list.addMousePress(Qt::LeftButton, Qt::NoModifier);
list.addDelay(1000);
list.addMouseRelease(Qt::LeftButton, Qt::NoModifier);
QWidget* widget = QApplication::desktop();
list.simulate(widget);

->也没有成功。

是否有可能在应用程序中的桌面小部件上使用的QTestEventList在Mac上完全没有效果?或者我做错了什么...

帮助将不胜感激;-(提前感谢!

卢克

我从未尝试过使用 QTestEventList 在 Mac 上模拟鼠标事件。相反,我使用了Mac Quartz Event Services API。

下面的代码演示如何将鼠标移动到某个位置以及如何模拟单击事件:

#include <ApplicationServices/ApplicationServices.h>
// Move the mouse to the position x, y on the screen
int x = 26;
int y = 11;
CGEventRef mouseEv = CGEventCreateMouseEvent(
                    NULL, kCGEventMouseMoved,
                    CGPointMake(x, y),
                    kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, mouseEv);
CFRelease(mouseEv);
// Simulate a left mouse click down
CGEventRef mouseEv = CGEventCreateMouseEvent(
                NULL, kCGEventLeftMouseDown,
                getCursorPosition(),
                kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, mouseEv);
CFRelease(mouseEv);
// Simulate a left mouse click up
CGEventRef mouseEv = CGEventCreateMouseEvent(
                NULL, kCGEventLeftMouseUp,
                getCursorPosition(),
                kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, mouseEv);
CFRelease(mouseEv);