- (void) keyDown: (NSEvent *) 事件不起作用
下面是示例代码。
- (void) keyDown: (NSEvent *) event
{
NSString *chars = [event characters];
unichar character = [chars characterAtIndex: 0];
if (character == 27) {
NSLog (@"ESCAPE!");
}
}
我是否需要在 InterfaceBuilder 中设置任何委托或任何类型的绑定?
帮助赞赏...
Below is the sample code.
- (void) keyDown: (NSEvent *) event
{
NSString *chars = [event characters];
unichar character = [chars characterAtIndex: 0];
if (character == 27) {
NSLog (@"ESCAPE!");
}
}
Should I need to set any delegate in InterfaceBuilder or any kinda binding??
Help Appreciated...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在可可中,只有视图参与此事件的响应者链。 所以你应该重写一些视图方法。 最简单的方法是找出哪个视图是您要处理的特定事件的第一响应者并使用它。
窗口直接向第一个响应者发送
keyDown(with: )
,该响应者可以处理它或向上传递到响应者链。 并非所有观点都会忽略这些事件。 NSCollectionView 不传递按键事件。 它会播放碰撞声。您想要处理的密钥也可能是等效密钥 在此处了解更多信息。 如果是这样,您应该重写 PerformKeyEquivalent(with: ) 方法来接收此类事件。 该事件与 keyDown 事件不同,它从窗口传递到所有子视图,直到有人处理它们。
如前所述, NSCollectionView keyDown(with: ) 方法不会将按键事件传递到响应者链上。 要在其中一个超级视图中处理此类事件,您应该首先在集合视图中覆盖它,并通过调用 self.nextResponder?.keyDown(with: event) 来手动发送事件,以处理您想要自己处理的此类事件。
In cocoa only views participate in responder chain for this event. So you should override a some view method. The easy way is to find out what view is first responder for particular event you want to handle and use it.
window sends
keyDown(with: )
stright to first responder which could handle it or pass up to responder chain. Not all views pass the events up. NSCollectionView doesn't pass the key event. It plays a bump sound instead.It is also possible that a key you want to handle is a Key equivalent read more here. If so you should override performKeyEquivalent(with: ) method to receive this type of events instead. This events unlike keyDown events passed down from the window to the all subviews until someone handle them.
As mentioned NSCollectionView keyDown(with: ) method do not pass the key events up the responder chain. To handle such events in one of it's super views you should override it in collection view first and send event manually by calling self.nextResponder?.keyDown(with: event) for such events that you want to handle by yourself.
keyDown
需要在其对象设置为第一响应者的NSView
子类中重写。 响应者链应该传递事件,但为了确保你得到它,请确保你的对象是第一响应者。keyDown
needs to be overridden in anNSView
subclass whose object is set as first responder. The responder chain should pass down the event, but to be sure you get it, make sure that your object is first responder.