OSX 中的 KeyDown 操作?
我已经为我的 keydown 操作完成了所有代码,但我不知道如何处理我访问的每个站点似乎都会略过的第一响应者。谁能告诉我如何设置它来识别可可 Objectivec 中的按键操作?
谢谢
I have the code all done for my keydown actions, but i dont know what to do with the first responder that every site i go to seems to skim over. Can anyone tell me how to set it up to recognise keydown actions in cocoa objectivec?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,
keyDown:
是一个事件消息,而不是一个操作消息。请注意,它的参数是 NSEvent,而不是某种 UI 对象(例如 NSControl 或 NSMenuItem)。操作消息沿着响应者链向下传递,在这种情况下,“第一响应者”并不特殊。每个响应者都会将其不知道如何处理的任何操作消息传递给下一个响应者。这就是“响应者链”。第一响应者就是位于响应者链头部的任何响应者,即第一个。您只需要处于该链中,位于任何不知道如何响应传递给它的操作的后面。
但由于这是一条事件消息,所以情况有所不同。您需要成为关键视图,这是第一响应者。
这就是全部内容。您需要在视图中响应
keyDown:
消息(以及可能相关的消息),并且该视图需要是接收该消息的第一个响应者。NSResponder 类参考 和 Cocoa 事件处理指南 将告诉您更多信息。
First,
keyDown:
is an event message, not an action message. Note that its argument is an NSEvent, not a UI object of some sort (such as an NSControl or NSMenuItem).Action messages go down the responder chain, in which case the “first responder” is not special. Each responder will hand any action message it doesn't know how to handle off to its next responder. This is the “responder chain”. The first responder is simply whatever responder is at the head of the responder chain—i.e., is first. You would simply need to be in that chain, behind anything that doesn't know how to respond to the action being passed down it.
But since this is an event message, things are different. You need to be the key view, which is the first responder.
And that's all there is to it. You need to respond to the
keyDown:
message (and possibly related ones) in a view, and that view needs to be the first responder to receive the message.The NSResponder class reference and Cocoa Event-Handling Guide will tell you more.
这是我所做的并且效果很好。 (适用于 macOS Sierra 的 Swift 3)
现在我还覆盖了这些:
以及这些:
这应该可以帮助您入门。
Here is what I've done and it works well. (Swift 3 for macOS Sierra)
Now I also override these:
And these:
That should get you started.