次のようなイベント タップを作成することをお勧めします (この回答から抜粋) applicationDidFinishLaunching:
。
CGEventMask emask;
CFMachPortRef myEventTap;
CFRunLoopSourceRef eventTapRLSrc;
// We only want one kind of event at the moment: Left mouse down
emask = CGEventMaskBit(kCGEventLeftMouseDown);
// Create the Tap
myEventTap = CGEventTapCreate (
kCGSessionEventTap, // Catch all events for current user session
kCGTailAppendEventTap, // Append to end of EventTap list
kCGEventTapOptionListenOnly, // We only listen, we don't modify
emask,
&myEventTapCallback,
NULL // We need no extra data in the callback
);
// Create a RunLoop Source for it
eventTapRLSrc = CFMachPortCreateRunLoopSource(
kCFAllocatorDefault,
myEventTap,
0
);
// Add the source to the current RunLoop
CFRunLoopAddSource(
CFRunLoopGetCurrent(),
eventTapRLSrc,
kCFRunLoopDefaultMode
);
通常はマウスイベントを無視するようにウィンドウを設定します -[myWindow setIgnoresMouseEvents: YES];
次に、イベントタップは、「キャッチ」したいマウスクリックを探します-次のようなものです:
static CGEventRef myEventTapCallback (
CGEventTapProxy proxy,
CGEventType type,
CGEventRef event,
void * refcon
) {
CGPoint mouseLocation;
// If we would get different kind of events, we can distinguish them
// by the variable "type", but we know we only get mouse moved events
mouseLocation = CGEventGetLocation(event);
// Figure out if the mouse is clicking on something we want to "catch"
if (/* want this click */)
[myWindow setIgnoresMouseEvents: NO];
// Pass on the event, we must not modify it anyway, we are a listener
return event;
}
マウス イベントが完了したら、マウス イベントを無視するようにウィンドウを戻します。