<p>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. Can anyone point me in the right direction or supply some first-hand help? </p>

[{'answer_id': 11312, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 5, 'selected': True, 'text': "<p>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.</p>\n\n<p>So, to detect a key press in a text view, make sure your controller is the text view's delegate, and then implement this:</p>\n\n<pre><code>- (void)keyUp:(NSEvent *)theEvent\n</code></pre>\n\n<p>Note that this is an inherited NSResponder method, not a NSTextView method.</p>\n"}, {'answer_id': 11999, 'author': 'Chris Hanson', 'author_id': 714, 'author_profile': 'https://Stackoverflow.com/users/714', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n\n<p>For example, when someone asks me how to capture key events in an NSText<strong>Field</strong> what they really want to know is how to validate input in the field.  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.</p>\n\n<p>So given that example, what are you really trying to accomplish?</p>\n"}, {'answer_id': 12874, 'author': 'Brian Warshaw', 'author_id': 1344, 'author_profile': 'https://Stackoverflow.com/users/1344', 'pm_score': 1, 'selected': False, 'text': "<p>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.</p>\n\n<p>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. 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. So if I implement this in my controller:</p>\n\n<pre><code>- (void)textDidChange:(NSNotification *)aNotification;\n</code></pre>\n\n<p>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.</p>\n"}, {'answer_id': 13088, 'author': 'alextgordon', 'author_id': 1165750, 'author_profile': 'https://Stackoverflow.com/users/1165750', 'pm_score': 3, 'selected': False, 'text': "<p>Just a tip for syntax highlighting:</p>\n\n<p>Don't highlight the whole text view at once - it's <em>very</em> 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.</p>\n\n<p>Instead you need to highlight the visible text which is done like this:</p>\n\n<pre><code>NSRect visibleRect = [[[textView enclosingScrollView] contentView] documentVisibleRect];\nNSRange visibleRange = [[textView layoutManager] glyphRangeForBoundingRect:visibleRect inTextContainer:[textView textContainer]];\n</code></pre>\n\n<p>Then you feed visibleRange to your highlighting code.</p>\n"}]
['https://Stackoverflow.com/questions/11291', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1344/']