简体   繁体   English

禁用ESC和Command。 在OSX Cocoa应用程序中

[英]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. 我制作了一个小可可应用程序,该应用程序会打开IKPictureTaker并按设置按钮将图片保存到文件中。 Currently, if you press esc or Command . 当前,如果您按esc或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 . 您可能必须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. 如果您希望/需要降低到较低的水平,请参阅CGEvent API。 Using it, you'd create a tap and swallow/modify specific events. 使用它,您将创建一个水龙头并吞咽/修改特定事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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