问题描述
我正在使用以下代码来模拟鼠标的点击:
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);
}
通过更改以下内容,我可以使用基本相同的代码来进行Control-Click(右键单击):
I can use basically the same code to do a control-click (right click) by changing:
kCGEventLeftMouseDown
kCGEventLeftMouseDown
kCGEventLeftMouseUp
kCGEventLeftMouseUp
kCGMouseButtonLeft
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);
}
但是,双击该怎么办?我尝试发送2个leftclicks并连续两次调用PostMouseEvent(),但是没有运气.您如何执行双击?
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?
谢谢!
推荐答案
查看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.
所以基本上:
- 创建事件
- 将点击计数设置为2
- 将事件类型设置为mousedown并发送
- 将事件类型设置为mouseup并发送
- 重复3和4
void doubleClick(int clickCount) {
CGEventRef theEvent = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, point, kCGMouseButtonLeft);
CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, clickCount);
CGEventPost(kCGHIDEventTap, theEvent);
CGEventSetType(theEvent, kCGEventLeftMouseUp);
CGEventPost(kCGHIDEventTap, theEvent);
CGEventSetType(theEvent, kCGEventLeftMouseDown);
CGEventPost(kCGHIDEventTap, theEvent);
CGEventSetType(theEvent, kCGEventLeftMouseUp);
CGEventPost(kCGHIDEventTap, theEvent);
CFRelease(theEvent);
}
这篇关于使用CGEventCreateMouseEvent()双击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!