简体   繁体   English

NSTextfield + NSMenu和第一响应者

[英]NSTextfield + NSMenu and first responder

I'm trying to implement my own autocomplemention system (result is pull from an sqlite database) 我正在尝试实现自己的自动补全系统(结果是从sqlite数据库中提取的)

I've set up a NSTextField and the appropriate delegate. 我已经设置了NSTextField和适当的委托。 Each time the text in the NSTextField change, it call - (void)controlTextDidChange:(NSNotification *)aNotification method 每次NSTextField中的文本更改时,它都会调用- (void)controlTextDidChange:(NSNotification *)aNotification方法

It work fine, in this method I build a menu programtically and finally I call/show it with that 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 is my NSTextField and menu is my NSMenu . address是我的NSTextFieldmenu是我的NSMenu The problem is that the menu take the focus, so you can type only 1 letter in the text field and then you can't type text anymore because the menu is now the first responder. 问题在于菜单是焦点,因此您只能在文本字段中键入1个字母,然后再也不能键入文本,因为菜单现在是第一响应者。

My questions is how to show the menu and keep the text field as first responder so you can type in it while the menu is reloaded at each text change in the field. 我的问题是如何显示菜单并保持文本字段为第一响应者,以便您可以在字段中每次更改文本时重新加载菜单时键入文本。

It should be like in Safari or chrome address bar in fact for example. 实际上,它应该像在Safari或chrome地址栏中。

I don't believe this is possible with NSMenu. 我认为NSMenu不可能做到这一点。 NSMenu implementation is controlled by the system at a quite low level, and it is designed to take keyboard focus. NSMenu实现是由系统在相当低的级别上控制的,旨在将键盘焦点对准。 What you need is to create your own view, or window, that looks somewhat like a menu, but is not using NSMenu. 您需要创建自己的视图或窗口,看起来像菜单,但不使用NSMenu。 Notice for example that that the menu in the chrome address bar does not look like a standard NSMenu. 例如,请注意,chrome地址栏中的菜单看起来不像标准的NSMenu。 You need to create a view that will appear and draw, and receive callbacks or notifications to update as the user types, but will not take keyboard focus. 您需要创建一个将显示和绘制的视图,并接收回调或通知以在用户键入时进行更新,但不会占据键盘焦点。 There are methods on NSView (NSResponder actually) that control whether a view accepts first responder status. NSView(实际上是NSResponder)上有一些方法可以控制视图是否接受第一响应者状态。

As mgorbach stated it is not really possible with NSMenu. 正如mgorbach所说,使用NSMenu并不是真的可能。 I've switched to NSTableView and customized my textfield. 我已经切换到NSTableView并自定义了我的文本字段。 The textfield forward the up and down arrow to the Table view and that work fine ! 文本字段将向上和向下箭头前进到“表格”视图,这样可以正常工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM