您可以在 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
希望对你有所帮助