title: Detecting Key Press/ Key Down iOS
body: <p>I am developing a library, which will be used in an app,
  where I need to know the timestamp of key pressed (key down and key up moments), in the iOS(iPhone) virtual keyboard.</p>
  <p>I was trying to do it by inheritance from <code>UIViewController</code> and override <code>pressesBegan</code> and <code>pressesEnded</code> functions like <a href="https://www.hackingwithswift.com/example-code/uikit/how-to-detect-keyboard-input-using-pressesbegan-and-pressesended" rel="nofollow noreferrer">here</a>, but it turns out its only working for <strong>external physical keyboards</strong> (so like &gt;1% users of iPhones?)</p>
  <p>I was trying to do this by getting all the textFields:</p>
  <pre><code>func getAllTextFields(fromView view: UIView)-&gt; [UITextField] {
      return view.subviews.flatMap { (view) -&gt; [UITextField] in
          if view is UITextField {
              return [(view as! UITextField)]
          } else {
              return getAllTextFields(fromView: view)
          }
      }.flatMap({$0})
  }
  </code></pre>
  <p>and use of <code>.addTarget</code> method but there's no function which provide information about &quot;key down&quot; moment:</p>
  <pre><code>textFields.forEach{($0.addTarget(self, action: #selector(textFieldEditingBegin),
                                           for: .editingDidBegin))}
  
  textFields.forEach{($0.addTarget(self, action: #selector(textFieldDidChange),
                                           for: .editingChanged))}
  
  textFields.forEach{($0.addTarget(self, action: #selector(textFieldDidEnd),
                                           for: .editingDidEnd))}
  </code></pre>
  <p>The <code>.editingChanged</code> method can only be translated to &quot;key up&quot;.
  The other ones, tells us about whole textField, so the moment when user &quot;tap&quot; on it and not the moment when he &quot;tap&quot; the letter in the virtual keyboard.</p>
  <p>I have not found anything useful for me in the <code>NotificationCenter</code>, <code>UIResponder</code> or <code>UITextFieldDelegate</code>.</p>
  <p>So im asking if anyone knows how can I retrieve such information from a iOS keyboard,
  im not very familiar with Extension mechanism and maybe there is some other stuff im able to do in my scenario.</p>
  <p>Thanks.</p>