Re: design: NSTextView subclass with custom key binding
Re: design: NSTextView subclass with custom key binding
- Subject: Re: design: NSTextView subclass with custom key binding
- From: Douglas Davidson <email@hidden>
- Date: Thu, 2 May 2002 10:02:56 -0700
On Thursday, May 2, 2002, at 06:03 AM, jerome LAURENS wrote:
I currently have a custom NSTextView subclass and I want it to respond
to user defined key bindings maybe overriding the standard one.
This subclass is the only text view in my app that will respond to
those key bindings, all others will behave in a standard way.
Any suggestion for the proper design to implement this feature?
Here is the normal sequence when a text view receives key events:
NSTextView's keyDown: passes events to interpretKeyEvents:, which is
where they enter key binding and input management. They come out either
as insertText: or as doCommandBySelector: (see NSResponder.h for these
three methods).
For example, an enter or return will (with the standard key bindings)
end up using doCommandBySelector: to call insertNewline: on the
NSTextView. If the textview is not a field editor, this will call
insertText: to insert a newline character. If it is a field editor (for
example, when editing a textfield) this will end editing instead. An
arbitrary text view can be made to act in either of these ways by
calling setFieldEditor:.
A tab or backtab will (with the standard key bindings) end up using
doCommandBySelector: to call insertTab: or insertBacktab: on the
NSTextView. If the textview is not a field editor, insertTab: will call
insertText: to insert a tab character; insertBacktab: will do nothing.
If it is a field editor, this will end editing instead.
I usually recommend avoiding subclassing if possible. You can, for
example, implement the text view delegate method
- (BOOL)textView:(NSTextView *)aTextView
doCommandBySelector:(SEL)aSelector;
and check the value of aSelector, and at that point do something other
than the default.
However, if for some other reason you are already working in a text view
subclass, you can override doCommandBySelector: and/or insertText:.
Douglas Davidson
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.