听到 keyUp 事件?:重写 NSView 方法
更新:我现在正在重写 NSView 子类中设置为第一响应者的 NSView keyUp 方法,如下所示,但我仍然没有看到它被调用的证据。
@implementation svsView
- (BOOL)acceptsFirstResponder {
return YES;
}
- (void)keyUp:(NSEvent *)event {
//--do key up stuff--
NSLog(@"key up'd!");
}
@end
--原帖-- 我是 Cocoa 和 Obj-C 的新手,我正在尝试从我的控制器类(其本身是 NSController 类型)的实现中执行 (void)keyUp: 。不过,我不确定这是否是放置它的正确位置。我有一系列类似的按钮,每个按钮都设置为唯一的等效键(IB 按钮属性),并且每个按钮都调用我的 (IBAction)keyInput 方法,该方法然后将每个键的标识传递到另一个对象。这运行得很好,但我还想跟踪每个键何时被释放。
--原始[坏]示例--
@implementation svsController
//init
//IBActions
- (IBAction)keyInput:(id)sender {
//--do key down stuff--
}
- (void)keyUp:(NSEvent *)event {
//--do key up stuff--
}
@end
失败后,我还尝试将 keyUp 作为 IBAction (而不是 void),就像用户定义的 keyInput 一样,并将其连接到 Interface Builder 中的相应按钮,但随后 keyUp 是仅在按键按下时调用,而在释放时不调用。 (我认为这会发生。)
请原谅我的笨蛋,但是我应该把这个方法放在另一个类中还是做一些不同的事情?但无论它在哪里,我都需要它能够访问控制器类拥有的对象。
感谢您的任何见解。
UPDATED: I'm now overriding the NSView keyUp method from a NSView subclass set to first responder like below, but am still not seeing evidence that it is being called.
@implementation svsView
- (BOOL)acceptsFirstResponder {
return YES;
}
- (void)keyUp:(NSEvent *)event {
//--do key up stuff--
NSLog(@"key up'd!");
}
@end
--ORIGINAL POST--
I'm new to Cocoa and Obj-C and am trying to do a (void)keyUp: from within the implementation of my controller class (which itself is of type NSController). I'm not sure if this is the right place to put it, though. I have a series of like buttons each set to a unique key equivalent (IB button attribute) and each calls my (IBAction)keyInput method which then passes the identity of each key onto another object. This runs just fine, but I also want to track when each key is released.
--ORIGINAL [bad] EXAMPLE--
@implementation svsController
//init
//IBActions
- (IBAction)keyInput:(id)sender {
//--do key down stuff--
}
- (void)keyUp:(NSEvent *)event {
//--do key up stuff--
}
@end
Upon fail, I also tried the keyUp as an IBAction (instead of void), like the user-defined keyInput is, and hooked it up to the appropriate buttons in Interface Builder, but then keyUp was only called when the keys were down and not when released. (Which I kind of figured would happen.)
Pardon my noobery, but should I be putting this method in another class or doing something differently? Wherever it is, though, I need it be able to access objects owned by the controller class.
Thanks for any insight you may have.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSResponder 子类(例如 NSView 的自定义子类)处理按键事件。它们甚至可以处理更抽象级别的事件,例如“向左/向右移动”、“插入换行符”等。然后它们可以向控制器发送消息以做出相应响应。
NSResponder subclasses (such as a custom subclass of NSView) handle key events. They even handle more abstract-level events such as "move left/right", "insert newline", etc. They can then send a message to a controller to respond accordingly.
哇!我想我已经成功了。我只需要使用对 IB 中的类的 NSView/NSWindow 引用来实例化我的子类。实际上并没有在 UI 中创建它的实例。过去的几个小时真是太糟糕了!但我一路上学到了一两件事。 ;)
Wow! I think I've nailed it. I just needed to instantiate my subclass with a NSView/NSWindow reference to the class in IB. Wasn't actually creating an instance of it in the UI. The past several hours down the crapper! Sans that I learned a thing or two along the way. ;)