I made a little cocoa app that brings up an IKPictureTaker and saves a picture to a file if you press the set button. Currently, if you press esc or Command . the window picture taker will close. Is there a way to disable this behavior?
Asked
Active
Viewed 1,110 times
3 Answers
2
You need to insert yourself somewhere in the responder chain in time to catch the escape key down event, and disable it. You may have to subclass IKPictureTaker
. The snippet below should help you (source).
- (void)keyDown:(NSEvent *)event {
if ([event keyCode] == 53) {
NSLog(@"Escape has been pressed");
}
}

Johan Kool
- 15,637
- 8
- 64
- 81
2
Another approach is to hide the close and Cancel buttons, so they can't be pressed:
IKPictureTaker *taker = [IKPictureTaker pictureTaker]; [taker setStyleMask:0]; //disable close button for(NSView *aView in [[taker contentView] subviews]){ if([aView isKindOfClass:[NSButton class]]){ NSButton *aButton = (NSButton*)aView; if([aButton action] == @selector(cancelButton:)) [aButton setHidden:YES]; } }

Vincent Gable
- 3,455
- 23
- 26
-
The close button is disabled in my app, what I am asking about is how to prevent it from being close when someone pressed the esc key or command . – Mike2012 Dec 08 '09 at 18:28
-
1It's the cancel or close buttons that are acting on the esc/cmd-. So disabling them gives you the behavior you want. But if you want the button visible, but without that key equivalent, then instead of doing [aButton setHidden:YES] do, [aButton setKeyEquivalent:@""] – Vincent Gable Dec 08 '09 at 20:26
-
1This doesn't work any more on 10.9 Mavericks. If I'm missing something, please let me know. Thanks. – Chuck H May 01 '14 at 01:51
0
If you want/need to drop down to the low level, see the CGEvent API. Using it, you'd create a tap and swallow/modify specific events.

justin
- 104,054
- 14
- 179
- 226