Re: Preventing textfield in table from clicking default button
Re: Preventing textfield in table from clicking default button
- Subject: Re: Preventing textfield in table from clicking default button
- From: Ken Thomases <email@hidden>
- Date: Sat, 16 Aug 2014 23:03:13 -0500
On Aug 16, 2014, at 9:53 PM, Seth Willits <email@hidden> wrote:
> When editing in a NSTextField and return is pressed, it fires the text field's action and also sends performKeyEquivalent: to the window, which clicks the default button. Usually that's just fine, but in the case where a view-based table view has editable NSTextFields in it, this makes no sense.
>
> Anybody figured out how to prevent this?
I recommend that you do a variant of the technique outlined in Technical Q&A QA1454: How to make NSTextField accept tab, return and enter keys.
https://developer.apple.com/library/mac/qa/qa1454/_index.html
The general mechanism is to implement the control delegate method -control:textView:doCommandBySelector: and alter the response to the "insertNewline:" selector.
You're not interested in inserting the newline into the text, but you can adjust the response to the return key in other ways. If you return YES, you stop any further processing of the selector. That allows you to have complete control over the response. As to what you do before returning YES, I think it should be sufficient to make another view (or the window itself) the window's first responder using -[NSWindow makeFirstResponder:].
Probably, making the table view itself the first responder makes the most sense, although perhaps there's an intervening ancestor view between the text field and the table view that would make more sense in your case. Or perhaps you could just delegate the logic to -[NSWindow selectNextKeyView:], to make it as though the user had hit Tab instead of return. (I would be amused by the irony of subverting the Tech Q&A by translating an invocation of -insertNewline: into an invocation of -insertTab:, too.)
Regards,
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