Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Cocoa Button look like is pressed, programmatically

I have some key events, that correspond to buttons also. What property/method to I have to set/call for a button to look depressed (change state?) for say half a second?

like image 368
James Hartt Avatar asked Nov 06 '22 11:11

James Hartt


1 Answers

The way I solved this is I set the NSButton to a type of 'Push On Push Off' and then used the following code in my key event handler:

NSButton *button = [self.superview viewWithTag:event.keyCode];
if (button != nil && button.state == NSOffState) {
    [button performClick:event];
    [button performSelector:@selector(performClick:) withObject:event afterDelay:0.5];
}

This will highlight the button as if the user had clicked on it, and then it will click on it again in half a second.

like image 97
Ben L. Avatar answered Nov 11 '22 06:11

Ben L.