Drawing NSControl when windows are key or not

I have an NSControl routine, and I want to change the drawing when the control is not included in keyWindow. The problem is that I do not see any property that reflects this state (I tried the property enabled, but it was not).

Simply put, can I distinguish between these two states?

disabledenabled

+3
source share
1 answer

You can use the NSWindow property keyWindow, and if you want to check if your control is the first responder for keyboard events, also check [[self window] firstResponder] == self. I do not believe that the keyWindowsupport KVO, but there is NSWindowDidBecomeKeyNotificationand NSWindowDidResignKeyNotificationthat you can listen to. For instance,

- (id)initWithFrame:(NSRect)frameRect;
{
    if ( self = [super initWithFrame:frameRect] )
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(display) name:NSWindowDidResignKeyNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(display) name:NSWindowDidBecomeKeyNotification object:nil];
    }

    return self;
}

- (void)drawRect:(NSRect)aRect;
{
    if ( [[self window] isKeyWindow] )
    {
    // one way...
    }
    else
    {
    // another way!
    }
}

- (void)dealloc;
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResignKeyNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidBecomeKeyNotification object:nil];
    [super dealloc];
}
+6
source

All Articles