• 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 Question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTextView keyDown Question


  • Subject: Re: NSTextView keyDown Question
  • From: Douglas Davidson <email@hidden>
  • Date: Tue, 20 Dec 2005 09:28:12 -0800


On Dec 20, 2005, at 2:30 AM, Nik Youdale wrote:

This is a relatively simple question i think, but basically i have an NSTextView and I want to be able to check out the keys that the user presses when they type. If the key is not a 'special key' (defined in the program - for example: "(" or "/" or return), then i want that key to get passed on to the NSTextView like normal.

I have tried subclassing NSTextView and over-riding the keyDown method, but when i do that, nothing gets passed on to the textView.


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 Question (From: Nik Youdale <email@hidden>)

  • Prev by Date: Re: Adding password for a file to keychain
  • Next by Date: Re: Adding password for a file to keychain
  • Previous by thread: Re: NSTextView keyDown Question
  • Next by thread: Implementing a scrollable icon-table
  • Index(es):
    • Date
    • Thread