Autocomplete word as you are typing

  • I am trying to figure out how I would go about doing an autocompletion
    of a word as you are typing.  I am trying to do something like any
    autocomplete done on the web where you start typing in your
    information and if there is a 1 to 1 match against a list it suggests
    a completion to the word.  I know there is the
    "textView:completions:forPartialWordRange:indexOfSelectedItem:"
    delegate method, but I want something that will happen as the person
    types without hitting the escape key.

    Does anyone have any examples, advice, delegate methods to look at.

    Thanks in advance
  • On Feb 6, 2008, at 10:50 PM, Chris Schmitt wrote:

    > I am trying to figure out how I would go about doing an
    > autocompletion of a word as you are typing.  I am trying to do
    > something like any autocomplete done on the web where you start
    > typing in your information and if there is a 1 to 1 match against a
    > list it suggests a completion to the word.  I know there is the
    > "textView:completions:forPartialWordRange:indexOfSelectedItem:"
    > delegate method, but I want something that will happen as the
    > person types without hitting the escape key.
    >
    >
    > Does anyone have any examples, advice, delegate methods to look at.

    Maybe something like this (not tested, just wrote it in Mail):

    [[NSNotificationCenter defaultNotificationCenter] addObserver:self
                               selector:@selector(controlTextDidChange:)
                                name: NSControlTextDidChangeNotification
                                 object: myTextControl];

    [NSNotificationCenter defaultNotificationCenter] addObserver:self
                               selector:@selector(controlTextDidEnd:)
                                name: NSControlTextDidEndEditingNotification
                                 object: myTextControl];

    ...

    - (void) controlTextDidChange:(NSNotification  *) inNotification
    {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];

    [NSObject performSelector:@selector(myCompletionRoutine:)
    withObject: [[inNotification userInfo] objectForKey:@"NSFieldEditor"]
    afterDelay:1.0f];
    }

    - (void) controlTextDidEnd:(NSNotification  *) inNotification
    {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    }

    - (void) myCompletionRoutine:(NSTextFieldCell *) inTextFieldCell
    {
    // Do your completion


    }
  • > I know there is the
    > "textView:completions:forPartialWordRange:indexOfSelectedItem:"
    > delegate method, but I want something that will happen as the person
    > types without hitting the escape key.

    I did something similar in the following way:

    First I subclassed an NSTextField and overrode its keyUp method like so:

    -(void)keyUp:(NSEvent *)event{
          int keyCode = [event.characters characterAtIndex:0];
          if (keyCode != 13 && keyCode != 9 && keyCode != 127 && keyCode !
    = NSLeftArrowFunctionKey && keyCode != NSRightArrowFunctionKey) {
              [self.currentEditor complete:self];
          }
          [super keyUp:event];
    }

    The if block checks to make sure that the key pressed is not
    backspace, delete, an arrow key, or any of that stuff that you really
    don't want completion to happen after, then calls complete: on its
    field editor (this is, as far as I know, equivalent to pressing ESC).

    If you want to complete using the default dictionary words, that
    should be all you have to do. If, however, you want to suggest your
    own words, you should also override
    textView:completions:forPartialWordRange:indexOfSelectedItem: to
    return something useful. See:

    o http://tinyurl.com/3xrdhf
    o http://tinyurl.com/3xrdhf
    o http://tinyurl.com/3xrdhf

    Cheers,
    -Joshua Emmons
  • On 2008/02/07, at 6:50, Chris Schmitt wrote:
    > I am trying to figure out how I would go about doing an
    > autocompletion of a word as you are typing.  I am trying to do
    > something like any autocomplete done on the web where you start
    > typing in your information and if there is a 1 to 1 match against a
    > list it suggests a completion to the word.  I know there is the
    > "textView:completions:forPartialWordRange:indexOfSelectedItem:"
    > delegate method, but I want something that will happen as the
    > person types without hitting the escape key.
    > Does anyone have any examples, advice, delegate methods to look at.

    Invoke [yourTextView completion:nil] somewhere in your code.

    Also you can find a sample code written by Douglas Davidson in
    CocoaBuilder archive.
    http://www.cocoabuilder.com/archive/message/cocoa/2005/7/18/142172

    Satoshi
    -----------------------------------------------------
    Satoshi Matsumoto <satoshi...>
    816-5 Odake, Odawara, Kanagawa, Japan 256-0802
  • Sorry, I mistook the method name.

    On 2008/02/07, at 6:50, Chris Schmitt wrote:
    > I am trying to figure out how I would go about doing an
    > autocompletion of a word as you are typing.  I am trying to do
    > something like any autocomplete done on the web where you start
    > typing in your information and if there is a 1 to 1 match against a
    > list it suggests a completion to the word.  I know there is the
    > "textView:completions:forPartialWordRange:indexOfSelectedItem:"
    > delegate method, but I want something that will happen as the
    > person types without hitting the escape key.
    > Does anyone have any examples, advice, delegate methods to look at.

    Invoke [yourTextView complete:nil] somewhere in your code.

    Also you can find a sample code written by Douglas Davidson in
    CocoaBuilder archive.
    http://www.cocoabuilder.com/archive/message/cocoa/2005/7/18/142172

    Satoshi
    -----------------------------------------------------
    Satoshi Matsumoto <satoshi...>
    816-5 Odake, Odawara, Kanagawa, Japan 256-0802