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?
Asked
Active
Viewed 1,519 times
2 Answers
1
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.

Ben L.
- 787
- 10
- 18
1
I believe the button cell's -setHighlighted:
method controls whether the button looks pressed or not. You may also need to call -setNeedsDisplay:
on the button after changing it, and it's possible that the button will change its cell's highlighted state by itself, so I'm afraid you may need to fiddle around to get this working.
(I have to admit, though, that I'm not entirely certain about any of this.)

Becca Royal-Gordon
- 17,541
- 7
- 56
- 91
-
Yes, that did it: [(NSButtonCell *)yourButton.cell setHighlighted:YES]; no need for setNeedsDisplay. Thanks. – Borzh Jul 09 '15 at 16:35