使用CGEventCreateMouseEvent

使用CGEventCreateMouseEvent

本文介绍了使用CGEventCreateMouseEvent()双击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来模拟鼠标的点击:

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.

所以基本上:

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

这篇关于使用CGEventCreateMouseEvent()双击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:32