如何处理 Cocoa 应用程序中的箭头键事件?
如何处理 Cocoa 应用程序中的箭头键事件?
How to handle arrow key event in Cocoa app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何处理 Cocoa 应用程序中的箭头键事件?
How to handle arrow key event in Cocoa app?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
请参阅此代码。我假设该类是 NSView 的子类。
视图应该是接收事件的第一响应者。也许需要这段代码来支持这一点。
要使用此方法,该类应该是 NSResponder 的子类。请参阅其他答案处理,无需子类化 NSResponder。
See this code. I assumed the class is subclass of
NSView
.A view should be a first responder to receive events. Maybe this code will be required to support that.
To use this method, the class should be subclass of
NSResponder
. See the other answer handling without subclassing NSResponder.就我而言,我希望呈现的 NSViewController 子类能够以最小的努力侦听箭头键事件以进行导航。这是我找到的最佳解决方案,与 Josh Caswell 的答案略有不同。
定义一个事件监视器(可选),可以在你的 NSViewController 子类本地 .m
然后开始监视事件,例如在 viewDidLoad 中。
在不需要时删除监视器(假设您定义了它)
In my case I wanted a presented NSViewController subclass to be able to listen to arrow key events for navigation with minimal effort. Here's the best solution I've found, a slight variation of Josh Caswell's answer.
Define an event monitor (optional), can be locally in your NSViewController subclass .m
Then start monitoring events, for example in viewDidLoad.
To remove the monitor when not required (assuming you defined it)