gpt4 book ai didi

macos - Cocoa 有鼠标移动模式识别器吗?

转载 作者:行者123 更新时间:2023-12-03 16:42:58 25 4
gpt4 key购买 nike

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

有没有库可以完成这个任务?

谢谢!

最佳答案

您可以在 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

希望对您有帮助

关于macos - Cocoa 有鼠标移动模式识别器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12268714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com