KeyDown和Cocoa Sample

编程入门 行业动态 更新时间:2024-10-26 06:38:28
本文介绍了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文档进一步解释。

应用程序类扩展了一个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

本文发布于:2023-11-27 01:43:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636058.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:KeyDown   Cocoa   Sample

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!