NSTextfield + NSMenu and the first responder

I am trying to implement my own autocompletion system (the result is obtained from the sqlite database)

I installed NSTextField and the corresponding delegate. Each time the text in NSTextField changes, it calls the - (void)controlTextDidChange:(NSNotification *)aNotification

It works fine, in this method I build the menu programmatically, and finally I call / display it with this code:

  NSRect frame = [address frame]; NSPoint menuOrigin = [[address superview] convertPoint:NSMakePoint(frame.origin.x, frame.origin.y+frame.size.height-25) toView:nil]; NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseDown location:menuOrigin modifierFlags:NSLeftMouseDownMask // 0x100 timestamp:0 windowNumber:[[address window] windowNumber] context:[[address window] graphicsContext] eventNumber:0 clickCount:1 pressure:1]; [NSMenu popUpContextMenu:menu withEvent:event forView:address]; 

Where address my NSTextField and menu is my NSMenu . The problem is that the menu is focused, so you can enter only one letter in the text box, and then you can no longer enter text, because now the menu is the first responder.

My questions are to show the menu and save the text field as the first responder so that you can enter it when the menu reloads every time the text in the field is changed.

This should be, for example, in the address bar of Safari or Chrome.

+4
source share
2 answers

I do not think this is possible with NSMenu. The implementation of NSMenu is controlled by the system at a rather low level and is designed to focus on the keyboard. You need to create your own view or window that looks like a menu but does not use NSMenu. Note, for example, that the menu in the chrome address bar is not like standard NSMenu. You need to create a view that will be displayed and drawn, as well as receive callbacks or notifications for updating by type of user, but will not deal with the keyboard. There are NSView methods (actually NSResponder) that control whether the view accepts the status of the first responder.

+3
source

As Mgorbach said, with NSMenu this is not possible. I switched to NSTableView and set up my text box. The text box directs the up and down arrows to the Table view and works great!

0
source

All Articles