获取 NSTextField 的 keyDown 事件
在 xcode 最新版本中,我试图获取 NSTextField 的 keyDown 事件。 然而,尽管遵循了互联网上的多个教程(代表、控制器......),我仍然无法收到它。
对我来说有什么简单的提示吗?
谢谢 !
In xcode last version, I am trying to get the keyDown event of an NSTextField.
However, despite following multiple tutorials on the internet (delegates, controllers...), I still can't receive it.
Any easy hint for me ?
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我厌倦了人们以其他方式做这件事的所有无答案的人,所以我埋头苦干,想出了一种方法来完成这项工作。这不是直接使用 keydown 事件,而是使用块中的 keydown。这种行为正是我想要的。
子类化文本字段
.h
.m
在成为第一响应者设置中,本地事件
在文本字段编辑结束后删除该事件
另外,如果您使用 ARC,我注意到您可能需要将 textview 字符串分配给 stringValue。我对 stringValue 进行了 nslog,并且该值被保留。如果没有 nslog,我必须将通知对象字符串分配给 stringValue 以防止其被释放。
I got sick of all the non answers to do it some other way people so I put my nose down and figured out a way to make this work. This isn't using keydown event directly but it is using the keydown in the block. And the behavior is exactly what I wanted.
Subclass the text field
.h
.m
In the become first responder setup a local event
remove the event once the textfield editing ends
Also if you're using ARC I noticed you might need to assign the textview string to the stringValue. I nslog'd the stringValue and the value was retained. Without the nslog I had to assign the notification object string to the stringValue to keep it from getting released.
您可以继承 NStextField 并使用适用于 NSTextField 的 keyUp 。
英寸 .h
英寸 .m
You can subclass NStextField and use keyUp that works for NSTextField.
in .h
in .m
像这样将 UITextFieldDelegate 添加到您的 .h
然后您可以使用它来检测文本字段中的每个按键
返回 YES 以允许按下的字符插入到字段中,但您可以在此处添加您需要的任何代码。
Add UITextFieldDelegate to your .h like this
Then you can use this to detect every key press in a text field
Return YES to allow the character that was pressed to be inserted into the field but you can add whatever code you need in here.