简体   繁体   English

如何将键事件发送到没有焦点在swift中的NSTextField

[英]How to send a key event to an NSTextField that does not have focus in swift

I have a view that contains an NSTableView and an NSTextField. 我有一个包含NSTableView和NSTextField的视图。 The table view has focus. 表视图具有焦点。 I would like to use the text field as a filter for the table view. 我想使用文本字段作为表视图的过滤器。 Is there any way that I can send a key press event, which is captured by the table view, to the NSTextField? 有什么方法可以将表视图捕获的按键事件发送到NSTextField吗?

Here is the keyDown func as I have it, I would like to send theEvent to the text field in the default handler of the switch statement. 这是我拥有的keyDown函数,我想将theEvent发送到switch语句的默认处理程序中的text字段。

override func keyDown(theEvent: NSEvent) {
    let s   =   theEvent.charactersIgnoringModifiers!
    let s1  =   s.unicodeScalars
    let s2  =   s1[s1.startIndex].value
    let s3  =   Int(s2)
    switch s3 {
        case NSUpArrowFunctionKey:
            self.previous()
            return
        case NSDownArrowFunctionKey:
            self.next()
            return
        default:
            // this appends the text to the textField, Is there a way to send theEvent to the textField?
            textField.stringValue = textField.stringValue + s;

            // textField.keyDown(theEvent) // this does not work
            break
    }
}

I would like to not have to force the user to change focus to the text field in order to filter the table view results. 我希望不必强制用户将焦点更改为文本字段以过滤表格视图结果。

As far as I understand it the -keyDown: is sent to the firstResponder which in this case is the table view. 据我所知, -keyDown:被发送到firstResponder ,在这种情况下是表视图。

I don't like this idea and I would implement this is a different way, but you could try inserting the text field into the responder chain and a position above table view. 我不喜欢这个想法,我会实现这是一种不同的方式,但您可以尝试将文本字段插入响应器链和表视图上方的位置。 You would have to subclass the text field and implement mouseDown to capture the events and you would also have to change you table views mouse down implementation so it didn't capture the event for the text field. 您必须子类化文本字段并实现mouseDown以捕获事件,您还必须更改表格视图鼠标按下实现,因此它不会捕获文本字段的事件。 This is overly complicated. 这太复杂了。

Why not simply set the target/action of the text field and deal with user input outside of the event loop? 为什么不简单地设置文本字段的目标/动作并处理事件循环之外的用户输入?

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

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