处理 Return 键事件和自定义 NSView
我的 mac 应用程序中有一个自定义 NSView,我想在按下 Return 键时执行一个操作。
我的第一次尝试是找到该键的 NSResponder 操作消息(例如 ESC 键的 -complete: )。看起来是 –insertNewline: 但它不适用于 NSView。
第二个,覆盖 -keydown: 并检查事件的 keyCode。有效,但我注意到 keyCode 只是键盘中某个键的位置,不同的键盘(型号和语言)将为 Return 键返回不同的键码(正确吗?)。
最后是将我的视图子类化为 NSControl 并将选择器设置为在子类的 -awakeFromNib 方法中作为操作执行(也设置目标)。
这些都有效。有什么想法吗?
提前致谢
I have a custom NSView in my mac app and I want to do an action when Return key is pressed.
My first attempt was to find the NSResponder action message for this key (like -complete: for ESC key). It seems it is –insertNewline: but it doesn´t work for NSView.
Second one, override -keydown: and check the keyCode of the event. Works but I notice that the keyCode is just the position of a key in the keyboard and differents keyboards (models and languages) will return differents keycodes for Return key (is that correct?).
The last was to subclass my view as NSControl and set the selector to perfom as action (also setting the target) in the -awakeFromNib method of the subclass.
Any of this works. Any idea?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后 –insertNewline:效果完美。问题是我使用“if”和“-interpretKeyEvents:”覆盖 keydown(当条件不允许执行 insertNewline 时混合不良)...
Finally –insertNewline: works perfect. The problem it was that I override keydown using an "if" with "-interpretKeyEvents:" (bad mix when the condition didn´t let perform insertNewline)...