0

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. 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.

JULIANG
  • 37
  • 9
  • Do you mean dismissing the keyboard? – Ahmad F Aug 05 '18 at 06:59
  • Possible duplicate of [Simulate keypress using Swift](https://stackoverflow.com/questions/27484330/simulate-keypress-using-swift) – Ashraf Aug 05 '18 at 07:03
  • I disagree that this question is a duplicate of that one. That question is attempting to simulate a keypress, but this one is attempting to perform a specific action from an iOS keyboard extension – Michael Hulet Aug 05 '18 at 07:23
  • In iMessage, pressing the Return key with the normal system keyboard simply inserts a newline, and does not send the message – Michael Hulet Aug 05 '18 at 08:18

1 Answers1

1

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:

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

That way, you can just call your method to do it:

textDocumentProxy.return()
Michael Hulet
  • 3,253
  • 1
  • 16
  • 33
  • Thanks for the fast response. Unfortunately, insertText("/n") will just insert a new line. What I want to do is for the default system action in whatever app I am using the keyboard to take place. So, in iMessage, pressing return would send the message. In excel, it would move to the next cell. In safari it would submit the web address. etc, etc. – JULIANG Aug 05 '18 at 08:14
  • Backslash ("\"), not forward slash ("/"). I just tested, and it appears to work for me – Michael Hulet Aug 05 '18 at 08:15
  • Sorry for the typo. I did use Backslash, however, I am still just getting a new line. Will start the app from fresh again as I must have a rogue setting in there somewhere. – JULIANG Aug 05 '18 at 09:04
  • Ahah.!! When installed to my iPad, it works. With the simulator it just gives a new line. Thanks so much. I have spent days looking at this and started out using \n but in the simulator. It was there in front of me all the time. – JULIANG Aug 05 '18 at 09:09