• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSTextView keydown alternative?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >NSTextView keydown alternative? (From: Nik Youdale <email@hidden>)

  • Prev by Date: NSButton not responding to mouseEntered: ?
  • Next by Date: Re: WebConvertNSImageToCGImageRef Issues
  • Previous by thread: Re: NSTextView keydown alternative?
  • Next by thread: helper tool for authorization
  • Index(es):
    • Date
    • Thread