NSButton with mouse / up and down / up

I need an NSButton that gives me 2 events, one when the button is pressed (NSOnState) and one when the button is released (NSOffState), and so far this works with the mouse for me (intercepting mouseDown: event). But using the keyboard shortcut does not work, it starts NSOnState once, and then after a delay very often. Is there a way to get a button that starts NSOnState when pressed and NSOffState when released?

My current NSButton subclass looks like this and unfortunately works using a delegate:

-(void)awakeFromNib {
    [self setTarget:self];
    [self setAction:@selector(buttonAction:)];
}

-(void)mouseDown:(NSEvent *)theEvent {
    [_delegate button:self isPressed:YES];
    [super mouseDown:theEvent];
}

-(void)buttonAction:(id)sender {
    [_delegate button:self isPressed:NO];
}
+4
source share
1 answer

-sendActionOn::

[self.button sendActionOn: NSLeftMouseDownMask | NSLeftMouseUpMask];

. , , NSButton -performKeyEquivalent:, , , .

- (BOOL) performKeyEquivalent: (NSEvent *) anEvent
{
    if ([super performKeyEquivalent: anEvent])
    {
        [self sendAction: self.action to: self.target];
        return YES;
    }
    return NO;
}

, , ( NSButtonCell ) -highlight:withFrame:inView::

- (void)highlight:(BOOL)flag
        withFrame:(NSRect)cellFrame
           inView:(NSView *)controlView
{
    [super highlight: flag withFrame:cellFrame inView:controlView];

    if (flag)
    {
        // Action hasn't been sent yet.
    }
    else
    {
        // Action has been sent.
    }
}
+6

All Articles