用CoreGraphics点击鼠标,奇怪的是没有取消

时间:2014-10-22 05:18:57

标签: macos core-graphics

下面的代码点击了鼠标,但(我认为)并没有取消鼠标。我必须手动点击正确的鼠标按钮,然后再次点击,然后它会“解开”#34;。

主要功能中的两个注释行,用于中键或双击,也不起作用。

再现问题的单行:

echo '
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
#import <Foundation/Foundation.h>


void clickMouse(CGEventType mouseDown, CGEventType mouseUp, CGMouseButton mouseButton, UInt32 clickCount, CGEventFlags modifiers) {

  // current mouse position
  CGPoint mousePosition = CGEventGetLocation(CGEventCreate(NULL));

  // because?
  CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
  CGEventRef eventDown = CGEventCreateMouseEvent(source, mouseDown, mousePosition, mouseButton);
  CGEventRef eventUp   = CGEventCreateMouseEvent(source, mouseUp,   mousePosition, mouseButton);

  // necessary?
  CGEventSetType(eventDown, mouseDown);
  CGEventSetType(eventUp,   mouseUp);

  CGEventSetFlags(eventDown, modifiers);

  CGEventSetIntegerValueField(eventDown, kCGMouseEventClickState, clickCount);
  CGEventSetIntegerValueField(eventUp,   kCGMouseEventClickState, 0);

  CGEventPost(kCGHIDEventTap, eventDown);
  CGEventPost(kCGHIDEventTap, eventUp); // error here?

}

int main(int argc, char** argv)
{

  @autoreleasepool {
    clickMouse(kCGEventLeftMouseDown, kCGEventLeftMouseUp, kCGMouseButtonLeft, 1, 0);
  //clickMouse(kCGEventLeftMouseDown, kCGEventLeftMouseUp, kCGMouseButtonLeft, 2, 0);
  //clickMouse(kCGEventOtherMouseDown, kCGEventOtherMouseUp, kCGMouseButtonCenter, 1, 0);
  }

  return 0;
}

' > issue.m; gcc -ObjC -framework Cocoa  -o ./issue issue.m; ./issue
#

我在:

$ sw_vers
ProductVersion: 10.9.5

相关:

0 个答案:

没有答案