2

I was wondering if there was a way to open the iOs keyboard and programmatically press a button without interacting with the keyboard itself.

  • Which button are you trying to "press", there may be a way to call that method. For example if you are trying to "press" the change keyboards button to switch to the emoji keyboard, there is a method for programmatically calling the next keyboard to appear. etc – Albert Renshaw May 28 '16 at 18:55
  • @AlbertRenshaw what is that method you are referring to? – adrian1kat Sep 02 '17 at 02:50
  • @adrian1kat `setKeyboardType:` – Albert Renshaw Sep 02 '17 at 15:23
  • @adrian1kat you can also set `UITextInputMode` to any locale you want to change the user keyboard to, one of which is the emoji "language" keyboard. https://stackoverflow.com/a/44753740/2057171 – Albert Renshaw Sep 02 '17 at 15:26

4 Answers4

3

Yes, there is a way. There are actually several ways but all of them are undocumented and you definitely should not used them in production.

Generating a tap means generating a UIEvent (several of them, actually) and passing it to UIApplication.sendEvent(_:).

Unfortunately, UIEvent inner structure is undocumented but some people have already inspected it and you can google tools to synthesize events (e.g. https://github.com/n00neimp0rtant/ControlFreak). However note that the internal structure of UIEvent and UITouch can change between iOS versions (see iOS 9 UIEvent)

You can also use the internal UIAutomation API (see for example these generated headers) to generate a tap (that's what UI tests were using in previous releases) but again, this API is not publicly documented and it is loaded into the app only if the app is connected to Xcode. I am not sure how this private framework looks like on iOS 9.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
1

No there is not. You could send your application the event via the responder chain, but the keyboard is only accessible via touch for security reasons.

GlorySaber
  • 367
  • 2
  • 12
-1

I don't understand why you need to. If you have, let's say a UITextField, then just say,

textField.text += "a"

This can work with anything. You do not need to tap a keyboard button

Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61
-1
override func viewDidLoad() {
    super.viewDidLoad()


    textInput.becomeFirstResponder()

}

override func viewDidAppear(animated: Bool) {

    textInput.text = textInput.text ?? "" + ""

}
Wei Lu
  • 7
  • 2