Skip to content

Instantly share code, notes, and snippets.

@odrobnik
Created November 16, 2012 10:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save odrobnik/4086204 to your computer and use it in GitHub Desktop.
Save odrobnik/4086204 to your computer and use it in GitHub Desktop.
Forwarding keydown event to a search field
- (void)goToProductListWithEvent:(NSEvent *)event
{
self.selectedViewController = _productListViewController;
// make the search bar first responder
NSWindow *window = self.view.window;
[window makeFirstResponder:_productListViewController.searchField];
// set the keyDown character into the search field
_productListViewController.searchField.stringValue = [event characters];
// need to update filter because this didn't trigger the delegate method
[_productListViewController updateFilter:_productListViewController.searchField];
// put the cursor to the right of the character, otherwise it is selected
NSText *fieldEditor = [window fieldEditor:YES forObject:_productListViewController.searchField];
// do this on next run loop, otherwise it does not work
dispatch_async(dispatch_get_main_queue(), ^{
[fieldEditor setSelectedRange:NSMakeRange(1, 0)];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment