Re: Adding carriage-return behavior to NSTextView
Re: Adding carriage-return behavior to NSTextView
- Subject: Re: Adding carriage-return behavior to NSTextView
- From: Max Horn <email@hidden>
- Date: Wed, 26 Dec 2001 13:40:32 +0100
At 20:57 Uhr -0800 25.12.2001, Esteban wrote:
[...]
More or less that's what I'm using for an application I'm writing.
Anyways, maybe someone has a much neater and perhaps better example
:)
You can do it without subclassing and all the work (yes! cocoa rules
:), and instead add something like this to the delegate of the
textview:
- (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)aSelector
{
if (aSelector == @selector(insertNewline:))
{
// Fetch the current event
NSEvent *event = [NSApp currentEvent];
if ([event type] == NSKeyDown)
{
// Check whether the enter key was pressed
switch ([[event characters] characterAtIndex:0])
{
case NSEnterCharacter:
// Do what you like here;
return YES;
// Add more cases here, possible checking the
modifier keys, too
}
}
}
return NO;
}
If you notice in the KeyDownTextView.m's keyDown: method, the
KeyDownTextView is its own delegate, you can in code set the
delegate to something else and write your own optionreturnHit,
optionenterHit, returnHit, enterHit.
You can of course get the same by sending those messages above in the
corresponding cases.
Max
--
-----------------------------------------------
Max Horn
Software Developer
email: <
mailto:email@hidden>
phone: (+49) 6151-494890