Cocoa的任何鼠标移动模式识别器?

时间:2012-09-04 17:42:38

标签: macos cocoa

我需要一种适用于Cocoa的鼠标移动模式识别器。我特别需要的是识别鼠标“摇动”或某种圆周运动。我已经阅读了Protractor,但我想知道是否已经实施了某种类型的库。

我目前正在设置一个全局事件监视器来跟踪系统范围内的鼠标移动,但我需要能够识别特定的模式,如圆周运动,摇动和类似的模式。

_eventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:NSMouseMovedMask handler:^(NSEvent *eventoEntrada) {
    NSLog(@"Movement detected");

    NSPoint loc = [NSEvent mouseLocation];
    NSLog(@"x:%.2f y:%.2f",loc.x, loc.y);
}];

是否有任何图书馆可以完成这项任务?

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以在mac OS X中使用Quartz库 1-在applicationDidFinishLaunching方法中定义鼠标事件掩码,如

CFMachPortRef      mouseEventTap;
CGEventMask        mouseEventMask;


CFRunLoopSourceRef runLoopMouseSource;



// Create an event tap. We are interested in key presses.

mouseEventMask = (1 << kCGEventMouseMoved) ;

mouseEventTap = CGEventTapCreate(kCGSessionEventTap, kCGTailAppendEventTap, 0,
                                 mouseEventMask, mouseCGEventCallback, NULL);



if (!mouseEventTap) {
    fprintf(stderr, "failed to create event tap\n");
    exit(1);
}

// Create a run loop source.
runLoopMouseSource = CFMachPortCreateRunLoopSource(
                                              kCFAllocatorDefault, mouseEventTap, 0);



// Add to the current run loop.

CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopMouseSource,
                   kCFRunLoopCommonModes);

// Enable the event tap.
 CGEventTapEnable(mouseEventTap, true);

然后像这样实现回调函数mouseCGEventCallback

    CGEventRef mouseCGEventCallback(CGEventTapProxy proxy, CGEventType type,
                         CGEventRef event, void *refcon)
   {


    if (type == kCGEventMouseMoved)
    {
       //here you can detect any information you need from the event field key


       return event;
    }

 }

有关事件字段的详细信息,请查看此

https://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html#//apple_ref/c/tdef/CGEventField

希望对你有所帮助