使用 CGEventCreateMouseEvent() 执行双击
我使用以下代码来模拟鼠标单击:
void PostMouseEvent(CGMouseButton button, CGEventType type, const CGPoint point)
{
CGEventRef theEvent = CGEventCreateMouseEvent(NULL, type, point, button);
CGEventSetType(theEvent, type);
CGEventPost(kCGHIDEventTap, theEvent);
CFRelease(theEvent);
}
void LeftClick(const CGPoint point)
{
PostMouseEvent(kCGMouseButtonLeft, kCGEventMouseMoved, point);
NSLog(@"Click!");
PostMouseEvent(kCGMouseButtonLeft, kCGEventLeftMouseDown, point);
PostMouseEvent(kCGMouseButtonLeft, kCGEventLeftMouseUp, point);
}
我可以使用基本相同的代码来执行控制单击(右键单击),方法是将:
kCGEventLeftMouseDown
kCGEventLeftMouseUp
kCGMouseButtonLeft
更改为各自的“右键”事件。该函数看起来类似于:
void RightClick(const CGPoint point)
{
PostMouseEvent(kCGMouseButtonRight, kCGEventMouseMoved, point);
NSLog(@"Click Right");
PostMouseEvent(kCGMouseButtonRight, kCGEventRightMouseDown, point);
PostMouseEvent(kCGMouseButtonRight, kCGEventRightMouseUp, point);
}
但是,双击怎么样? 我尝试发送两次左键单击并连续两次调用 PostMouseEvent() 但没有成功。 如何执行双击?
谢谢!
I'm using the following code to simulate a click of the mouse:
void PostMouseEvent(CGMouseButton button, CGEventType type, const CGPoint point)
{
CGEventRef theEvent = CGEventCreateMouseEvent(NULL, type, point, button);
CGEventSetType(theEvent, type);
CGEventPost(kCGHIDEventTap, theEvent);
CFRelease(theEvent);
}
void LeftClick(const CGPoint point)
{
PostMouseEvent(kCGMouseButtonLeft, kCGEventMouseMoved, point);
NSLog(@"Click!");
PostMouseEvent(kCGMouseButtonLeft, kCGEventLeftMouseDown, point);
PostMouseEvent(kCGMouseButtonLeft, kCGEventLeftMouseUp, point);
}
I can use basically the same code to do a control-click (right click) by changing:
kCGEventLeftMouseDown
kCGEventLeftMouseUp
kCGMouseButtonLeft
to their respective "Right" events. The function looks something like:
void RightClick(const CGPoint point)
{
PostMouseEvent(kCGMouseButtonRight, kCGEventMouseMoved, point);
NSLog(@"Click Right");
PostMouseEvent(kCGMouseButtonRight, kCGEventRightMouseDown, point);
PostMouseEvent(kCGMouseButtonRight, kCGEventRightMouseUp, point);
}
But, how about a double click?
I tried sending 2 leftclicks and calling PostMouseEvent() twice in a row but no luck.
How do you perform a double click?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 CGEventSetIntegerValueField(event, kCGMouseEventClickState, clickCount)。此外,即使将 clickCount 设置为 2,在某些情况下,对于旧版应用程序,您可能也必须执行 2 个事件。
所以基本上:
编辑:
Look into
CGEventSetIntegerValueField(event, kCGMouseEventClickState, clickCount)
. Also, even after setting the clickCount to 2, you may have to perform 2 events in some cases, for legacy apps.So basically:
Edit:
这是模拟双击的示例代码。
请注意,
CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, 2);
在第一组向下、向上点击之后执行。This is sample code to simulate a double click.
Notice that
CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, 2);
goes after the first set of down,up clicks.