简体   繁体   English

在NSTextView中捕获关键事件的最佳方法?

[英]Best way to capture key events in NSTextView?

I'm slowly learning Objective-C and Cocoa, and the only way I see so far to capture key events in Text Views is to use delegation, but I'm having trouble finding useful documentation and examples on how to implement such a solution. 我正在慢慢学习Objective-C和Cocoa,到目前为止我在文本视图中捕获关键事件的唯一方法是使用委托,但是我很难找到有关如何实现这种解决方案的有用文档和示例。 Can anyone point me in the right direction or supply some first-hand help? 任何人都可以指出我正确的方向或提供一些第一手帮助?

Generally, the way you implement it is simply to add the required function to your view's controller, and set its delegate. 通常,实现它的方法只是将所需的函数添加到视图的控制器中,并设置其委托。 For example, if you want code to run when the view loads, you just delegate your view to the controller, and implement the awakeFromNib function. 例如,如果希望在加载视图时运行代码,则只需将视图委派给控制器,然后实现awakeFromNib函数。

So, to detect a key press in a text view, make sure your controller is the text view's delegate, and then implement this: 因此,要在文本视图中检测按键,请确保您的控制器是文本视图的委托,然后执行以下操作:

- (void)keyUp:(NSEvent *)theEvent

Note that this is an inherited NSResponder method, not a NSTextView method. 请注意,这是一个继承的NSResponder方法,而不是NSTextView方法。

Just a tip for syntax highlighting: 只是语法高亮的提示:

Don't highlight the whole text view at once - it's very slow. 不要立刻突出显示整个文本视图 - 它非常慢。 Also don't highlight the last edited text using -editedRange - it's very slow too if the user pastes a large body of text into the text view. 也不要使用-editedRange突出显示最后编辑的文本 - 如果用户将大量文本粘贴到文本视图中,它也会非常慢。

Instead you need to highlight the visible text which is done like this: 相反,您需要突出显示如下所示的可见文本:

NSRect visibleRect = [[[textView enclosingScrollView] contentView] documentVisibleRect];
NSRange visibleRange = [[textView layoutManager] glyphRangeForBoundingRect:visibleRect inTextContainer:[textView textContainer]];

Then you feed visibleRange to your highlighting code. 然后将visibleRange提供给突出显示的代码。

It's important to tell us what you're really trying to accomplish — the higher-level goal that you think capturing key events in an NSTextView will address. 告诉我们您真正想要实现的目标非常重要 - 您认为捕获NSTextView中的关键事件的更高级别目标将解决这个问题。

For example, when someone asks me how to capture key events in an NSText Field what they really want to know is how to validate input in the field. 例如,当有人问我如何捕获NSText 字段中的关键事件时,他们真正想知道的是如何验证字段中的输入。 That's done by setting the field's formatter to an instance of NSFormatter (whether one of the formatters included in Cocoa or a custom one), not by processing keystrokes directly. 这是通过将字段的格式化程序设置为NSFormatter的一个实例(无论是Cocoa中包含的格式化程序还是自定义格式化程序)来完成的,而不是直接处理键击。

So given that example, what are you really trying to accomplish? 所以,举个例子,你真正想要完成什么?

I've done some hard digging, and I did find an answer to my own question. 我做了一些艰苦的挖掘,我确实找到了自己问题的答案。 I'll get at it below, but thanks to the two fellas who replied. 我会在下面得到它,但感谢两位回答的人。 I think that Stack Overflow is a fantastic site already--I hope more Mac developers find their way in once the beta is over--this could be a great resource for other developers looking to transition to the platform. 我认为Stack Overflow已经是一个很棒的网站 - 我希望更多的Mac开发人员在测试结束后找到他们的方式 - 对于希望过渡到平台的其他开发人员来说,这可能是一个很好的资源。

So, I did, as suggested by Danny, find my answer in delegation. 所以,我按照丹尼的建议,在代表团中找到了答案。 What I didn't understand from Danny's post was that there are a set of delegate-enabled methods in the delegating object, and that the delegate must implement said events. 我在Danny的帖子中没有理解的是委托对象中有一组支持委托的方法,并且委托必须实现所述事件。 And so for a TextView, I was able to find the method textDidChange, which accomplished what I wanted in an even better way than simply capturing key presses would have done. 因此对于TextView,我能够找到textDidChange方法,它以一种更好的方式完成了我想要的工作,而不仅仅是捕获按键操作。 So if I implement this in my controller: 所以如果我在我的控制器中实现它:

- (void)textDidChange:(NSNotification *)aNotification;

I can respond to the text being edited. 我可以回复正在编辑的文本。 There are, of course, other methods available, and I'm excited to play with them, because I know I'll learn a whole lot as I do. 当然还有其他可用的方法,我很高兴和他们一起玩,因为我知道我会像我一样学到很多东西。 Thanks again, guys. 伙计们,再次感谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM