How to intercept keystrokes from within the field editor of an NSTextField?

谁说我不能喝 提交于 2019-12-05 12:40:14

I believe I have achieved greater clarity with regard to the workings of NSTextField its delegates and its fieldEditor and its delegates. Yes, each one has its own delegate and the NSTextField is automatically set up as the delegate of the fieldEditor.

I was adding a delegate to the host NSTextField. This will NOT get called when the fieldEditor is firstResponder, which was what I was concerned with. This is also not the important delegate for the scenario above.

What appears to be useful is making the NSTextField conform to the NSTextViewDelegate protocol and specifically overriding the method:

 func textView(textView: NSTextView, shouldChangeTextInRange affectedCharRange: NSRange, replacementString: String) -> Bool

to trap various keypresses.

The notification in textDidChange(notification: NSNotification) is not of use in this scenario because it is reported AFTER the keypress.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!