Search code examples
iphoneobjective-ccocoa-touchuigesturerecognizertouchesbegan

How to detect touches on the iPhone onscreen keyboard


I'm trying to detect how long a user is touching a key on a keyboard when she is typing, so that, for instance, I popup a dialog when a certain key is pressed for a long enough time.

Detecting this for a regular view is trivial with measuring the time between touchesEnded and touchesBegan. However, I don't receive any events from the keyboard. Any ideas on how one can receive touchesEnded and touchesBegan events from the keyboard?


Solution

  • You cannot add what you want to the system-provided keyboard. However, you could achieve what you want by implementing your own custom keyboard, which could have whatever methods you wanted to handle long key presses. See previous this previous question as one example of how to approach implementing a custom keyboard.

    When implementing this, rather than examining touchesEnded and touchesBegan to identify when a long touch occurred, you should the iOS-provided UILongPressGestureRecognizer.