How to make a transparent NSView subclass handle mouse events?

cocoa

Solution

As far as I know, click events to transparent portions of windows aren't delivered to your application at all, so none of the normal event-chain overrides (i.e -hitTest:, -sendEvent:, etc) will work. The only way I can think of off the top of my head is to use Quartz Event Taps to capture all mouse clicks and then figure out if they're over a transparent area of your window manually. That, frankly, sounds like a huge PITA for not much gain.

Problem

The problem I have a transparent `NSView` on a transparent `NSWindow`. The view's `drawRect:` method draws some content (`NSImage`s, `NSBezierPath`s and `NSString`s) on the view but leaves parts of it transparent. Clicking on the regions of the view that have been drawn on invokes the usual mouse event handling methods (`mouseDown:` and `mouseUp:`). Clicking on the transparent areas gives focus to whatever window is behind my transparent window. I would like to make parts of the transparent region clickable so that accidentally clicking between the elements drawn on my view does not cause the window to lose focus. Solutions already attempted - Overriding the `NSView`'s `hitTest:` method. Found that `hitTest:` was only called when clicking on a non-transparent area of the view. - Overriding the `NSView`'s `opaqueAncestor` method. Found that this was not called when clicking on any part of the view. - Filling portions of the transparent area with `[NSColor clearColor]` in the `drawRect:` method, and with an almost-but-not-quite-transparent colour. This had no effect. - Experimented with the `NSTrackingArea` class. This appears to only add support for `mouseEntered:`, `mouseExited:`, `mouseMoved:`, and `cursorUpdate:` methods, not `mouseUp:` and `mouseDown:`.

Original source