简体   繁体   English

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

[英]How to simulate mouse move and mouse click on Mac using C or C++

I am trying to simulate mouse move and mouse click on Mac using C or C++.我正在尝试使用 C 或 C++ 在 Mac 上模拟鼠标移动和鼠标点击。

But unfortunately I don't find any Libraries for the same.但不幸的是,我没有找到任何相同的库。

I have seen windows.h (works only for Windows) and also swinput (works for linux)我见过 windows.h(仅适用于 Windows)和 swinput(适用于 linux)

Is there anything like that for Mac? Mac有类似的东西吗?

CGPostMouseEvent has been deprecated in SnowLeopard. CGPostMouseEvent在 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'.

Mouse button 0 is the primary button on the mouse.鼠标按钮 0 是鼠标上的主按钮。 Mouse button 1 is the secondary mouse button (right).鼠标按钮 1 是辅助鼠标按钮(右)。 Mouse button 2 is the center button, and the remaining buttons are in USB device order.鼠标键 2 为中心键,其余按键按 USB 器件顺序排列。

kCGEventLeftMouseDown
kCGEventLeftMouseUp
kCGEventRightMouseDown
kCGEventRightMouseUp
kCGEventMouseMoved
kCGEventLeftMouseDragged
kCGEventRightMouseDragged

are now at your disposal.现在随时为您服务。

My recommendation is that you check how the Mac ports of VNC do it.我的建议是你检查 VNC 的 Mac 端口是如何做到的。

    // this is for macos only 

    #include<iostream>
    #include<ApplicationServices/ApplicationServices.h>
    void mouse_mover(int x, int y)
    {
          CGPoint mypoint =CGPointMake(x,y);
          typedef uint32_t CGDirectDisplayID;
          CGDirectDisplayID display_id =CGMainDisplayID();
          CGDisplayMoveCursorToPoint(display_id,mypoint);
   }


   int main()
   {
       int x,y;
       std::cout<<"Enter the point where you want to move your mouse"<<std::endl;
      std::cin>>x>>y;
      mouse_mover(x,y);
      return 69; // return your favourite integer.
    }
    // compile using : g++ program_name.cpp -framework ApplicationServices
    // ./a.out
  

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

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