如何使用 C 或 C++ 在 Mac 上模拟鼠标移动和鼠标点击

4

我希望使用C或C ++在Mac上模拟鼠标移动和鼠标点击。

但不幸的是,我找不到相应的库。

我看过windows.h(仅适用于Windows)和swinput(适用于Linux)。

是否有类似于Mac的东西?

2个回答

2

CGPostMouseEvent在SnowLeopard中已经被弃用。您可以使用类似以下的替代方法:

CGEventRef mouseDownEv = CGEventCreateMouseEvent (NULL,kCGEventLeftMouseDown,pt,kCGMouseButtonLeft);
CGEventPost (kCGHIDEventTap, mouseDownEv);

CGEventRef mouseUpEv = CGEventCreateMouseEvent (NULL,kCGEventLeftMouseUp,pt,kCGMouseButtonLeft);
CGEventPost (kCGHIDEventTap, mouseUpEv );

CGEventRef CGEventCreateMouseEvent( 
    CGEventSourceRef source,        // The event source may be taken from another event, or may be NULL.
    CGEventType mouseType,          // `mouseType' should be one of the mouse event types. 
    CGPoint mouseCursorPosition,    // `mouseCursorPosition'  should be the position of the mouse cursor in global coordinates. 
    CGMouseButton mouseButton);     // `mouseButton' should be the button that's changing state; 
                                    // `mouseButton'  is ignored unless `mouseType' is one of 
                                    // `kCGEventOtherMouseDown', `kCGEventOtherMouseDragged', or `kCGEventOtherMouseUp'.

鼠标按键0是鼠标上的主要按钮。 鼠标按键1是次要的鼠标按钮(右键)。 鼠标按键2是中心按钮,其余按钮按照USB设备顺序排列。

kCGEventLeftMouseDown
kCGEventLeftMouseUp
kCGEventRightMouseDown
kCGEventRightMouseUp
kCGEventMouseMoved
kCGEventLeftMouseDragged
kCGEventRightMouseDragged

现在您可以使用它们了。


1

我的建议是您可以查看一下 VNC 的 Mac 版本是如何实现的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接