Re: -insertNewline: and -insertLineBreak:, using the shift key modifier
Re: -insertNewline: and -insertLineBreak:, using the shift key modifier
- Subject: Re: -insertNewline: and -insertLineBreak:, using the shift key modifier
- From: Ken Thomases <email@hidden>
- Date: Wed, 11 Jun 2008 15:59:03 -0500
On Jun 11, 2008, at 3:10 PM, Keith Blount wrote:
In most text-based apps, you insert a newline by hitting return and
a line break by hitting shift-return.
Most text-based apps on the Mac? Because the default Cocoa text
bindings use Control-Return or Control-Enter for insertLineBreak:.
You can see this by examining /System/Library/Frameworks/
AppKit.framework/Resources/StandardKeyBinding.dict. See <http://developer.apple.com/documentation/Cocoa/Conceptual/EventOverview/TextDefaultsBindings/chapter_9_section_1.html#//apple_ref/doc/uid/20000468-CJBDEADF
> for some background.
There's a good (if somewhat dated) user-written page about the Cocoa
Text Binding system here <http://www.hcs.harvard.edu/~jrus/Site/Cocoa Text System.html
>.
This isn't the default behaviour of NSTextView, but both -
insertNewline: and -insertLineBreak: are available as actions.
However, using interface builder it is impossible to add shift-
return as a keyboard shortcut (it just gets entered as return
without the shift modifier), so the obvious way of implementing this
behaviour - having a menu item with -insertLineBreak: as its action
and applying shift-return as its keyboard shortcut - doesn't work. I
tried to get around this by using NSMenuItem's -
setKeyEquivalentModifierMask in my menu delegate (the app delegate).
This changed the shortcut in the menu to shift-return as desired,
but using the shortcut had no effect - a regular newline was entered.
This leads me to believe that shift-return is blocked somehow as a
shortcut by the text system, though I'm not sure how or why.
Actually, as noted in a thread a few weeks ago, NSApplication only
routes keyboard events through the menus if certain modifier keys are
down. Here's my contribution to that thread <http://www.cocoabuilder.com/archive/message/cocoa/2008/3/28/202525
>. You can read through the rest of the thread for more info.
The only thing I can think of is to override -insertNewline: in my
custom text view to call -insertLineBreak: if the shift key is held
down. This works, as follows:
- (void)insertNewline:(id)sender
{
if([[[selfwindow] currentEvent] modifierFlags] & NSShiftKeyMask)
[selfinsertLineBreak:sender];
else
[super insertNewline:sender];
}
My question is, is there anything wrong with doing it this way? Or
is there a better way of getting the expected line break keyboard
shortcut that I am missing, given that regular routes don't work?
Here's a very helpful post about customized key handling in text
views: <http://www.cocoabuilder.com/archive/message/cocoa/2008/6/6/209502
>.
Cheers,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden