KeyDown和Cocoa Sample [英] KeyDown and Cocoa Sample

查看:183
本文介绍了KeyDown和Cocoa Sample的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用Cocoa构建程序。我使用一个示例Apple应用程序记录来自网络摄像头的视频。我想通过捕获按键来开始和停止视频。我试图重写keydown事件,但我已经阅读,它不可能在 NSObject 。如何处理这种事件?



应用程序类扩展了一个 NSObject 类。



这是代码:

   - (void)keyDown: (NSEvent *)event {
NSLog(@Hi there);
NSString * characters = [event characters];
if([characters length]){
switch([characters characterAtIndex:0]){
case NSUpArrowFunctionKey:
NSLog(@Key UP);
break;
}
}
}


解决方案>



$ b

我试图覆盖Keydown事件,但我已经阅读了它在NSObject中是不可能的。 $ b

正确。只有回复者才能回复活动。


如何处理此类活动?




实施响应者。子类化NSWindow或NSWindowController将工作。请确保您将实际的窗口或窗口控制器作为子类的实例。



Cocoa文档进一步解释。



< blockquote>

应用程序类扩展了一个NSObject类。


为什么?通常,应用程序包的主类是NSApplication或其子类,并且没有很多好的理由来子类化NSApplication。


PS:什么是一本很好的书,开始学习MacOS编程?


我没有学习Hillegass的书,坚持苹果的文档),但它是一个非常受欢迎的建议,我已经阅读它,并可以告诉你这是好的。


I'm learning how to build programs with Cocoa. I'm using a sample Apple application that records video from a webcam. I'd like to start and stop the video by capturing the key press. I've tried to override the keydown event but I've read that it's not possible in an NSObject. How can I handle this kind of event?

The class of application extends a NSObject class.

This is the code:

- (void)keyDown:(NSEvent *)event {
  NSLog(@"Hi there");
  NSString *characters = [event characters];
  if ([characters length]) {
    switch ([characters characterAtIndex:0]) {
      case NSUpArrowFunctionKey:
      NSLog(@"Key UP");
      break;
    }
  }
}

解决方案

I've tried to override Keydown event but I've read that It's not possible in an NSObject.

Correct. Only a responder can respond to events.

How can I handle this kind of event?

Implement a responder. Subclassing NSWindow or NSWindowController will work. Make sure you make your actual window or window controller an instance of your subclass.

The Cocoa documentation explains further.

The class of application extends a NSObject class.

Why? Normally, the principal class of the application bundle is NSApplication or a subclass of that—and there aren't many good reasons to subclass NSApplication.

PS: What's a very good book to start learn MacOS Programming?

I didn't learn by the Hillegass book, myself (I stuck to Apple's docs), but it's a very popular recommendation and I have read it and can tell you it's good.

这篇关于KeyDown和Cocoa Sample的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆