简体   繁体   English

iOS Swift自定义键盘默认系统操作

[英]IOS Swift Custom Keyboard Default System Action

I am trying to make a button on my custom keyboard perform the default system action as if I had pressed return on a hardware keyboard. 我试图使自定义键盘上的按钮执行默认的系统操作,就像我按下硬件键盘上的return一样。 How do I do that please.? 我该怎么做?

This is not simulating a specific keypress, such as a new line. 这不是在模拟特定的按键,例如换行。 I want the default system action for whatever app I am using to take place. 我想要我正在使用的任何应用程序的默认系统操作。 iMessage, pressing return would send the message. iMessage,按回车键将发送消息。

Inserting a newline into the text document proxy does this automatically: 在文本文档代理中插入换行符会自动执行以下操作:

Swift: 迅速:

textDocumentProxy.insertText("\n")

Better yet, to make this more obvious, you could extend UITextDocumentProxy or its parent, UIKeyInput to add a method for this: 更好的是,为了使其更加明显,您可以扩展UITextDocumentProxy或其父级UIKeyInput来为此添加一个方法:

extension UIKeyInput{
    func `return`() -> Void{
         insertText("\n")
    }
}

That way, you can just call your method to do it: 这样,您只需调用您的方法即可:

textDocumentProxy.return()

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

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