2

我需要某种用于 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);
}];

有没有图书馆可以完成这项任务?

谢谢!

4

1 回答 1

1

您可以在 mac OS X 1 中使用 Quartz 库 - 在您的 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

希望对你有所帮助

于 2013-05-19T10:12:58.073 回答