执行使用CGEventCreateMouseEvent双击()(Performing a doubl

2019-07-19 18:47发布

我使用下面的代码来模拟鼠标点击:

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);
}

但是,怎么样双击? 我想送2个leftclicks和一排,但没有运气调用PostMouseEvent()的两倍。 你如何进行双击?

谢谢!

Answer 1:

考虑CGEventSetIntegerValueField(event, kCGMouseEventClickState, clickCount) 此外,即使使用clickCount设置为2后,您可能需要在某些情况下,执行2个事件,遗留应用程序。

所以基本上:

  1. 创建活动
  2. 集点击数为2
  3. 设置事件类型的mousedown和发送
  4. 设置事件类型mouseup和发送
  5. 重复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); 
}


Answer 2:

这是示例代码来模拟一个双击。

CGEventRef theEvent = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, CGPointMake(x, y), kCGMouseButtonLeft);  
CGEventPost(kCGHIDEventTap, theEvent);  
CGEventSetType(theEvent, kCGEventLeftMouseUp);  
CGEventPost(kCGHIDEventTap, theEvent);  

CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, 2);

CGEventSetType(theEvent, kCGEventLeftMouseDown);  
CGEventPost(kCGHIDEventTap, theEvent);  

CGEventSetType(theEvent, kCGEventLeftMouseUp); 
CGEventPost(kCGHIDEventTap, theEvent); 

CFRelease(theEvent); 

注意, CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, 2); 第一盘下,上点击后进入。



文章来源: Performing a double click using CGEventCreateMouseEvent()