tags:

views:

165

answers:

1

I'm working on a simple code editor in Cocoa. I have a text view, and I want it so that when the user hits return, the new line is indented the same as the previous line (like in Xcode or any other code editor).

My problem is that I can't figure out how to either intercept when the return/enter key is pressed or when a new line is created in the text view. What would be the easiest way to do this?

+1  A: 

You can override insertNewline: in the NSTextView if that's what you have. You can also implement the delegate method textView:doCommandBySelector:, which will be called with the insertNewline: selector as an argument when the user presses return.

Chuck
I'm really not sure how to do this. I made an NSView subclass and linked it to the textView. It doesn't pick up the command though. I apologize if this is a stupid question, or if I'm overlooking something obvious. I'm new to coding on OS X.
Your use of the term "textView" is quite confusing — and I don't know what it means to link an NSView subclass to one. If you have an NSTextView subclass, that would be where you override it.
Chuck