Search code examples
objective-cmacoscocoanstokenfieldnsresponder

How to make an NSControl (e.g., NSTokenField) ignore mouse events


Specifically, I'd like to make an NSTokenField ignore mouse events because I'm using it in a NSTableCellView just to display data in a tokenized way without allowing any editing.

Setting the token field's enabled = NO works, except that it greys out the tokens and makes it hard to read the text.

Setting the token field's editable = NO is pretty close to what I want—it prevents editing while preserving the token field's look—except that when I mouse over the tokens, they light up. If I could just prevent that, I'd be in business.

I suspect I need to subclass something and override some NSResponder methods, but not quite sure what to do. I tried subclassing NSTokenField and overriding mouseEntered: and mouseMoved: to do nothing, but that didn't work either.


Solution

  • After trying a lot of stuff, I finally got this to work based on Iulius Cæsar's suggestion.

    The trick was to subclass NSTextField and override trackingAreas:

    - (NSArray *)trackingAreas
    {
        return [NSArray array];
    }
    

    Simply deleting the field's tracking areas when creating it wasn't quite enough, because the field was in a scroll view and sometimes the tracking areas would be re-created.