简体   繁体   中英

Disabling ESC and Command . in an OSX Cocoa app

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?

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");
   }
}

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];
 }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM