Re: NSTextView keydown alternative?
Re: NSTextView keydown alternative?
- Subject: Re: NSTextView keydown alternative?
- From: Douglas Davidson <email@hidden>
- Date: Mon, 16 Jan 2006 08:45:41 -0800
On Jan 14, 2006, at 3:58 AM, Nik Youdale wrote:
I am building a custom view that consists of a textview (mainly)
which holds first responder status, and allows text input by the
user. I have implemented the
"textView:shouldChangeTextInRange:replacementString:" delegate
method to detect when characters are pressed, and to also detect
when keys such as return and tab are pressed so that i can disable
their action and/or invoke other methods.
What I need is someway of detecting when other keys are pressed
such as delete and arrow keys. I have tried overriding keydown, but
then i can't type into the textview. I need someway of detecting
these keystrokes and to still allow typing and the
"textView:shouldChangeTextInRange:replacementString:" delegate method.
Let me review again the path that keystrokes take in the text
system. First, generally speaking you do not want to override -
[NSTextView keyDown:] in most cases; keyDown: is the entry point for
raw keystrokes before they have been interpreted by input methods or
anything else, and if you are not careful in modifying it you can
easily break e.g. Japanese input.
NSTextView takes keystrokes as they come in to keyDown: and passes
them to the key binding system, from which they come back to the text
view either as insertText: (for ordinary keys) or as
doCommandBySelector: (for keys bound to various commands, such as
arrow keys, return, tab, etc.). At this point you can intervene,
either in a subclass, or with the delegate's
textView:doCommandBySelector:. Bear in mind that insertText: can
receive either an NSString (in the usual case) or an
NSAttributedString (for some input methods).
The delegate will also receive
textView:shouldChangeTextInRanges:replacementStrings: when the user
alters the text in any way--by ordinary keystrokes, by special
keystrokes that modify the text (e.g. return), by menu commands, by
pasting, by drag and drop, etc. This is the point to intervene if
you are not concerned about typing specifically, but about all user
modifications to the text.
In addition, there are other delegate methods such as
textView:willChangeSelectionFromCharacterRanges:toCharacterRanges:
and textView:shouldChangeTypingAttributes:toAttributes: that deal
with other consequences of user actions, such as changes in selection
or changes in the attributes that will be applied to subsequently
typed text. For example, arrow keys will modify the selection but
not the text; likewise, picking "bold" from a menu when there is no
text selected will modify the typing attributes but not the text.
Douglas Davidson
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden