我想编写一个C程序来自动执行在OSx中运行的程序中的点击(在桌面设置中)。
我首先尝试Using Quartz Event Services to simulate input events。但后来我遇到了这个问题:Simulating mouse clicks on Mac OS X does not work for some applications,答案在我的案例中并没有帮助。
CGEventRef click1_down = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, CGPointMake(posx, posy), kCGMouseButtonLeft);
CGEventSetIntegerValueField(click1_down, kCGMouseEventClickState, 0);
// This down click works about 5% of the time.
CGEventPost(kCGHIDEventTap, click1_down);
usleep(30000);
CGEventRef click1_up = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseUp, CGPointMake(posx, posy), kCGMouseButtonLeft);
CGEventSetIntegerValueField(click1_up, kCGMouseEventClickState, 1);
CGEventPost(kCGHIDEventTap, click1_up);
// I've tried every combination of CGEventSetIntegerValueField, usleep and CFRelease, nothing seems to help
// The only thing helping is repeating the line: "CGEventPost(kCGHIDEventTap, click1_down);" 100s of times,
// then the down click works about 80% of the time, still not acceptable
我现在转向此处建议的解决方案#3:How can Mac OS X games receive low-level keyboard input events?
(这也可能有助How can I simulate the touch events by IOHIDEvent?)
我通过在按键上发送鼠标来尝试Karabiner:
<item>
<name>Right Mousebutton</name>
<identifier>rightMouseButton</identifier>
<autogen>__KeyToKey__ KeyCode::H, PointingButton::LEFT</autogen>
</item>
这会在100%的时间内发送点击,但我想通过编写C代码来发送点击(以获得更好的控制权)。我很不确定,Karabiner似乎使用IOKit发送事件,所以我认为这应该适用于我的情况,如果我能够使用IOKit发送鼠标事件。
所以我的问题基本上是:我如何编写一个C程序来模拟鼠标左键单击IOKit? The documentation非常稀疏,我没有设法做到这一点。
我尝试从一些项目中获取灵感: