如何使用C或C ++在Mac上模拟鼠标移动和鼠标单击 [英] How to simulate mouse move and mouse click on Mac using C or C++

查看:557
本文介绍了如何使用C或C ++在Mac上模拟鼠标移动和鼠标单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C或C ++在Mac上模拟鼠标移动和鼠标单击.

I am trying to simulate mouse move and mouse click on Mac using C or C++.

但不幸的是,我没有找到相同的任何图书馆.

But unfortunately I don't find any Libraries for the same.

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

I have seen windows.h (works only for Windows) and also swinput (works for linux)

Mac是否有类似的东西?

Is there anything like that for Mac?

推荐答案

CGPostMouseEvent在SnowLeopard中已弃用.您可以将其替换为

CGPostMouseEvent has been deprecated in SnowLeopard. You can replace it with something like

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设备顺序排列.

Mouse button 0 is the primary button on the mouse. Mouse button 1 is the secondary mouse button (right). Mouse button 2 is the center button, and the remaining buttons are in USB device order.

kCGEventLeftMouseDown
kCGEventLeftMouseUp
kCGEventRightMouseDown
kCGEventRightMouseUp
kCGEventMouseMoved
kCGEventLeftMouseDragged
kCGEventRightMouseDragged

现在可供您使用.

这篇关于如何使用C或C ++在Mac上模拟鼠标移动和鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆