-1

I have an NSTextField using autocompletion. It's working well, but I want to trigger the text field's action method immediately when typing return, both when selecting an autocompletion item, and when no item is selected in the autocompletion list (I currently need to type return twice: to select/close the autocompletion, then to trigger the action). The first case is answered here, but I can't find any delegate or subclass method to implement for the second case.

Thanks

Community
  • 1
  • 1
Jérôme
  • 19
  • 4
  • What ?? please [read how to ask a good question](http://stackoverflow.com/help/how-to-ask) – meda Sep 05 '14 at 15:36
  • Providing requirements without showing any effort was once considered [off-topic](http://stackoverflow.com/help/on-topic). It is still [being debated now](http://meta.stackexchange.com/questions/215596/are-code-questions-without-an-attempt-now-on-topic), and many people consider it to be rude. – Scott Solmer Sep 05 '14 at 15:46

2 Answers2

0

Here's the delegate method that gets called after a word completion...

textView:completions:forPartialWordRange:indexOfSelectedItem: method in NSTextViewDelegate protocol

  • Thanks, but this is the method for sending the list of possible completion for a given partial word. I already have this working. My problem is that the NSTextField's action method is not called when the user hits return. NSTextView's insertCompletion:forPartialWordRange:movement:isFinal: is called when typing return to enter a selected completion, but nothing happens if the user hits return without selecting a completion. – Jérôme Sep 05 '14 at 16:14
  • Unfortunately, neither controlTextDidChange:, control:textView:doCommandBySelector: or insertCompletion:forPartialWordRange:movement:isFinal: are called in this case. The completion popup list is closed, but the text field is not changed as no completion has been chosen. – Jérôme Sep 08 '14 at 08:57
0

The only solution I found was to use method_exchangeImplementations to override the private class NSTextViewCompletionController, specifically the - (void)endDisplayAndComplete:(BOOL)complete method, then to examine [[(NSTextView *)[self currentTextView] window] currentEvent] to catch key down events for the return and enter keys.

Jérôme
  • 19
  • 4