Detect autocomplete in NSSearchField











up vote
0
down vote

favorite












I am trying to implement location suggestions within the searchfield. I can get all the suggestions, however I am not able to find out when user selects completion using the keyboard enter or mouse click.



The problem is that user needs to press enter to select completion (dissmiss completion menu window) and hit enter again to submit it to recents. I would love to avoid second hit enter keypress.



Autocompletion is off for "sends whole string" mode so that's a no go.



How to detect autocomplete selection on NSSearchField? Or how to trigger autocompletion with "sends whole search string"?



PS: There are undocumented notifications NSTextViewDidBeginDisplayingCompletions, NSTextViewDidEndDisplayingCompletions which might be used.



autocomplete nssearchfield



- (void)windowDidLoad {
[super windowDidLoad];

MKLocalSearchCompleter *searchCompleter = [[MKLocalSearchCompleter alloc] init];
searchCompleter.filterType = MKSearchCompletionFilterTypeLocationsOnly;
searchCompleter.delegate = self;
[self setSearchCompleter:searchCompleter];

[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = YES;
}];

[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = NO;
if ([[self searchCompleter] isSearching] == NO) {
[self search:nil];
}
}];
}

- (void)completerDidUpdateResults:(MKLocalSearchCompleter *)completer
{
[self setSearchAutocompletions:[completer results]];
[self displayAutocompletions];
}

- (void)completer:(MKLocalSearchCompleter *)completer didFailWithError:(NSError *)error
{
if (error) {
NSLog(@"%@",error);
}
}

- (IBAction)autocompleteSearch:(NSSearchField *)sender
{
if ([[sender stringValue] length] < 3) {
return;
}
if (self.isDisplayingCompletions) {
return;
}
NSString *searchString = [sender stringValue];
[[self searchCompleter] cancel];
[[self searchCompleter] setQueryFragment:searchString];
}



- (void)displayAutocompletions
{
if (self.isSearching && !self.completePosting) {
_completePosting = YES;
[[[self window] fieldEditor:YES forObject:[self searchField]] complete:nil];
_completePosting = NO;
}
}

- (void)controlTextDidEndEditing:(NSNotification *)obj
{
[self search:nil];
}

- (void)search:(id)sender
{
NSString *searchString = [[self searchField] stringValue];
if ([searchString length] <= 3) {
return;
}
MKLocalSearchCompletion *completionToSearch;
NSArray<MKLocalSearchCompletion *> *results = [[self searchCompleter] results];
for (MKLocalSearchCompletion *completion in results) {
if ([[completion title] isEqualToString:searchString]) {
completionToSearch = completion;
break;
}
}
// MKCoordinateRegion boundingRegion = [[self mapView] region];
MKLocalSearchRequest *request;
if (completionToSearch) {
request = [[MKLocalSearchRequest alloc] initWithCompletion:completionToSearch];
} else {
request = [[MKLocalSearchRequest alloc] init];
[request setNaturalLanguageQuery:searchString];
}
//[request setRegion:boundingRegion];
[[self localSearch] cancel];
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[self setLocalSearch:localSearch];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error);
} else {
[[self mapView] setRegion:[response boundingRegion]];
MKMapItem *item = [response.mapItems firstObject];
[[self searchField] setStringValue:[item name]];
MKPlacemark *placemark = [item placemark];

if (placemark) {
CLLocationCoordinate2D coordinate = placemark.coordinate;
NSLog(@"%@", placemark);
}
}
}];

}

#pragma mark NSControlTextEditingDelegate

- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index
{
NSMutableArray *matches = [[NSMutableArray alloc] init];
for (MKLocalSearchCompletion *completion in [self searchAutocompletions]) {
[matches addObject:[completion title]];
}
*index = -1;
return matches;
}

#pragma mark NSSearchFieldDelegate

- (void)searchFieldDidStartSearching:(NSSearchField *)sender
{
_searching = YES;
}

- (void)searchFieldDidEndSearching:(NSSearchField *)sender
{
_searching = NO;
}









share|improve this question
























  • Possible duplicate of How to make NSSearchField send action upon autocompletion?
    – Marek H
    Nov 9 at 10:08















up vote
0
down vote

favorite












I am trying to implement location suggestions within the searchfield. I can get all the suggestions, however I am not able to find out when user selects completion using the keyboard enter or mouse click.



The problem is that user needs to press enter to select completion (dissmiss completion menu window) and hit enter again to submit it to recents. I would love to avoid second hit enter keypress.



Autocompletion is off for "sends whole string" mode so that's a no go.



How to detect autocomplete selection on NSSearchField? Or how to trigger autocompletion with "sends whole search string"?



PS: There are undocumented notifications NSTextViewDidBeginDisplayingCompletions, NSTextViewDidEndDisplayingCompletions which might be used.



autocomplete nssearchfield



- (void)windowDidLoad {
[super windowDidLoad];

MKLocalSearchCompleter *searchCompleter = [[MKLocalSearchCompleter alloc] init];
searchCompleter.filterType = MKSearchCompletionFilterTypeLocationsOnly;
searchCompleter.delegate = self;
[self setSearchCompleter:searchCompleter];

[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = YES;
}];

[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = NO;
if ([[self searchCompleter] isSearching] == NO) {
[self search:nil];
}
}];
}

- (void)completerDidUpdateResults:(MKLocalSearchCompleter *)completer
{
[self setSearchAutocompletions:[completer results]];
[self displayAutocompletions];
}

- (void)completer:(MKLocalSearchCompleter *)completer didFailWithError:(NSError *)error
{
if (error) {
NSLog(@"%@",error);
}
}

- (IBAction)autocompleteSearch:(NSSearchField *)sender
{
if ([[sender stringValue] length] < 3) {
return;
}
if (self.isDisplayingCompletions) {
return;
}
NSString *searchString = [sender stringValue];
[[self searchCompleter] cancel];
[[self searchCompleter] setQueryFragment:searchString];
}



- (void)displayAutocompletions
{
if (self.isSearching && !self.completePosting) {
_completePosting = YES;
[[[self window] fieldEditor:YES forObject:[self searchField]] complete:nil];
_completePosting = NO;
}
}

- (void)controlTextDidEndEditing:(NSNotification *)obj
{
[self search:nil];
}

- (void)search:(id)sender
{
NSString *searchString = [[self searchField] stringValue];
if ([searchString length] <= 3) {
return;
}
MKLocalSearchCompletion *completionToSearch;
NSArray<MKLocalSearchCompletion *> *results = [[self searchCompleter] results];
for (MKLocalSearchCompletion *completion in results) {
if ([[completion title] isEqualToString:searchString]) {
completionToSearch = completion;
break;
}
}
// MKCoordinateRegion boundingRegion = [[self mapView] region];
MKLocalSearchRequest *request;
if (completionToSearch) {
request = [[MKLocalSearchRequest alloc] initWithCompletion:completionToSearch];
} else {
request = [[MKLocalSearchRequest alloc] init];
[request setNaturalLanguageQuery:searchString];
}
//[request setRegion:boundingRegion];
[[self localSearch] cancel];
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[self setLocalSearch:localSearch];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error);
} else {
[[self mapView] setRegion:[response boundingRegion]];
MKMapItem *item = [response.mapItems firstObject];
[[self searchField] setStringValue:[item name]];
MKPlacemark *placemark = [item placemark];

if (placemark) {
CLLocationCoordinate2D coordinate = placemark.coordinate;
NSLog(@"%@", placemark);
}
}
}];

}

#pragma mark NSControlTextEditingDelegate

- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index
{
NSMutableArray *matches = [[NSMutableArray alloc] init];
for (MKLocalSearchCompletion *completion in [self searchAutocompletions]) {
[matches addObject:[completion title]];
}
*index = -1;
return matches;
}

#pragma mark NSSearchFieldDelegate

- (void)searchFieldDidStartSearching:(NSSearchField *)sender
{
_searching = YES;
}

- (void)searchFieldDidEndSearching:(NSSearchField *)sender
{
_searching = NO;
}









share|improve this question
























  • Possible duplicate of How to make NSSearchField send action upon autocompletion?
    – Marek H
    Nov 9 at 10:08













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to implement location suggestions within the searchfield. I can get all the suggestions, however I am not able to find out when user selects completion using the keyboard enter or mouse click.



The problem is that user needs to press enter to select completion (dissmiss completion menu window) and hit enter again to submit it to recents. I would love to avoid second hit enter keypress.



Autocompletion is off for "sends whole string" mode so that's a no go.



How to detect autocomplete selection on NSSearchField? Or how to trigger autocompletion with "sends whole search string"?



PS: There are undocumented notifications NSTextViewDidBeginDisplayingCompletions, NSTextViewDidEndDisplayingCompletions which might be used.



autocomplete nssearchfield



- (void)windowDidLoad {
[super windowDidLoad];

MKLocalSearchCompleter *searchCompleter = [[MKLocalSearchCompleter alloc] init];
searchCompleter.filterType = MKSearchCompletionFilterTypeLocationsOnly;
searchCompleter.delegate = self;
[self setSearchCompleter:searchCompleter];

[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = YES;
}];

[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = NO;
if ([[self searchCompleter] isSearching] == NO) {
[self search:nil];
}
}];
}

- (void)completerDidUpdateResults:(MKLocalSearchCompleter *)completer
{
[self setSearchAutocompletions:[completer results]];
[self displayAutocompletions];
}

- (void)completer:(MKLocalSearchCompleter *)completer didFailWithError:(NSError *)error
{
if (error) {
NSLog(@"%@",error);
}
}

- (IBAction)autocompleteSearch:(NSSearchField *)sender
{
if ([[sender stringValue] length] < 3) {
return;
}
if (self.isDisplayingCompletions) {
return;
}
NSString *searchString = [sender stringValue];
[[self searchCompleter] cancel];
[[self searchCompleter] setQueryFragment:searchString];
}



- (void)displayAutocompletions
{
if (self.isSearching && !self.completePosting) {
_completePosting = YES;
[[[self window] fieldEditor:YES forObject:[self searchField]] complete:nil];
_completePosting = NO;
}
}

- (void)controlTextDidEndEditing:(NSNotification *)obj
{
[self search:nil];
}

- (void)search:(id)sender
{
NSString *searchString = [[self searchField] stringValue];
if ([searchString length] <= 3) {
return;
}
MKLocalSearchCompletion *completionToSearch;
NSArray<MKLocalSearchCompletion *> *results = [[self searchCompleter] results];
for (MKLocalSearchCompletion *completion in results) {
if ([[completion title] isEqualToString:searchString]) {
completionToSearch = completion;
break;
}
}
// MKCoordinateRegion boundingRegion = [[self mapView] region];
MKLocalSearchRequest *request;
if (completionToSearch) {
request = [[MKLocalSearchRequest alloc] initWithCompletion:completionToSearch];
} else {
request = [[MKLocalSearchRequest alloc] init];
[request setNaturalLanguageQuery:searchString];
}
//[request setRegion:boundingRegion];
[[self localSearch] cancel];
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[self setLocalSearch:localSearch];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error);
} else {
[[self mapView] setRegion:[response boundingRegion]];
MKMapItem *item = [response.mapItems firstObject];
[[self searchField] setStringValue:[item name]];
MKPlacemark *placemark = [item placemark];

if (placemark) {
CLLocationCoordinate2D coordinate = placemark.coordinate;
NSLog(@"%@", placemark);
}
}
}];

}

#pragma mark NSControlTextEditingDelegate

- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index
{
NSMutableArray *matches = [[NSMutableArray alloc] init];
for (MKLocalSearchCompletion *completion in [self searchAutocompletions]) {
[matches addObject:[completion title]];
}
*index = -1;
return matches;
}

#pragma mark NSSearchFieldDelegate

- (void)searchFieldDidStartSearching:(NSSearchField *)sender
{
_searching = YES;
}

- (void)searchFieldDidEndSearching:(NSSearchField *)sender
{
_searching = NO;
}









share|improve this question















I am trying to implement location suggestions within the searchfield. I can get all the suggestions, however I am not able to find out when user selects completion using the keyboard enter or mouse click.



The problem is that user needs to press enter to select completion (dissmiss completion menu window) and hit enter again to submit it to recents. I would love to avoid second hit enter keypress.



Autocompletion is off for "sends whole string" mode so that's a no go.



How to detect autocomplete selection on NSSearchField? Or how to trigger autocompletion with "sends whole search string"?



PS: There are undocumented notifications NSTextViewDidBeginDisplayingCompletions, NSTextViewDidEndDisplayingCompletions which might be used.



autocomplete nssearchfield



- (void)windowDidLoad {
[super windowDidLoad];

MKLocalSearchCompleter *searchCompleter = [[MKLocalSearchCompleter alloc] init];
searchCompleter.filterType = MKSearchCompletionFilterTypeLocationsOnly;
searchCompleter.delegate = self;
[self setSearchCompleter:searchCompleter];

[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = YES;
}];

[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = NO;
if ([[self searchCompleter] isSearching] == NO) {
[self search:nil];
}
}];
}

- (void)completerDidUpdateResults:(MKLocalSearchCompleter *)completer
{
[self setSearchAutocompletions:[completer results]];
[self displayAutocompletions];
}

- (void)completer:(MKLocalSearchCompleter *)completer didFailWithError:(NSError *)error
{
if (error) {
NSLog(@"%@",error);
}
}

- (IBAction)autocompleteSearch:(NSSearchField *)sender
{
if ([[sender stringValue] length] < 3) {
return;
}
if (self.isDisplayingCompletions) {
return;
}
NSString *searchString = [sender stringValue];
[[self searchCompleter] cancel];
[[self searchCompleter] setQueryFragment:searchString];
}



- (void)displayAutocompletions
{
if (self.isSearching && !self.completePosting) {
_completePosting = YES;
[[[self window] fieldEditor:YES forObject:[self searchField]] complete:nil];
_completePosting = NO;
}
}

- (void)controlTextDidEndEditing:(NSNotification *)obj
{
[self search:nil];
}

- (void)search:(id)sender
{
NSString *searchString = [[self searchField] stringValue];
if ([searchString length] <= 3) {
return;
}
MKLocalSearchCompletion *completionToSearch;
NSArray<MKLocalSearchCompletion *> *results = [[self searchCompleter] results];
for (MKLocalSearchCompletion *completion in results) {
if ([[completion title] isEqualToString:searchString]) {
completionToSearch = completion;
break;
}
}
// MKCoordinateRegion boundingRegion = [[self mapView] region];
MKLocalSearchRequest *request;
if (completionToSearch) {
request = [[MKLocalSearchRequest alloc] initWithCompletion:completionToSearch];
} else {
request = [[MKLocalSearchRequest alloc] init];
[request setNaturalLanguageQuery:searchString];
}
//[request setRegion:boundingRegion];
[[self localSearch] cancel];
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[self setLocalSearch:localSearch];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error);
} else {
[[self mapView] setRegion:[response boundingRegion]];
MKMapItem *item = [response.mapItems firstObject];
[[self searchField] setStringValue:[item name]];
MKPlacemark *placemark = [item placemark];

if (placemark) {
CLLocationCoordinate2D coordinate = placemark.coordinate;
NSLog(@"%@", placemark);
}
}
}];

}

#pragma mark NSControlTextEditingDelegate

- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index
{
NSMutableArray *matches = [[NSMutableArray alloc] init];
for (MKLocalSearchCompletion *completion in [self searchAutocompletions]) {
[matches addObject:[completion title]];
}
*index = -1;
return matches;
}

#pragma mark NSSearchFieldDelegate

- (void)searchFieldDidStartSearching:(NSSearchField *)sender
{
_searching = YES;
}

- (void)searchFieldDidEndSearching:(NSSearchField *)sender
{
_searching = NO;
}






objective-c macos cocoa mapkit nssearchfield






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 10:34

























asked Nov 8 at 16:46









Marek H

2,2821222




2,2821222












  • Possible duplicate of How to make NSSearchField send action upon autocompletion?
    – Marek H
    Nov 9 at 10:08


















  • Possible duplicate of How to make NSSearchField send action upon autocompletion?
    – Marek H
    Nov 9 at 10:08
















Possible duplicate of How to make NSSearchField send action upon autocompletion?
– Marek H
Nov 9 at 10:08




Possible duplicate of How to make NSSearchField send action upon autocompletion?
– Marek H
Nov 9 at 10:08

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212400%2fdetect-autocomplete-in-nssearchfield%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212400%2fdetect-autocomplete-in-nssearchfield%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Liste der Kulturdenkmale in Wilsdruff

Android Play Services Check