I am working on a small children’s game for my son that involves timing on the virtual keyboard.
Is possible to detect when a key on the virtual keyboard is pressed? I.e. get a touch notification (touchesBegan)? I do not need to know which key is pressed, just if and when the press action started.
It’s not possible to get the touch events directly from the virtual keyboard. However, you could use a
UITextFieldthat you place offscreen, so that it’s not visible and callbecomeFirstResponderto show the keyboard. Then, as Khomsan suggested, you could implement the delegate methodtextView:shouldChangeTextInRange:to be notified whenever a key is pressed.A cleaner possibility would be to write a custom
UIControlthat implements theUIKeyInputprotocol. You would only need to provide implementations forinsertText:,deleteBackwardandhasText(for this one, you can simply always returnYES). To make the keyboard visible, you would again have to callbecomeFirstResponderfor your custom control.Both of these methods have in common that you only will be notified when the key is released (so that text is entered), not when the touch actually begins. As I said, it’s not possible to get the
tochesBeganevent directly. If you need that, you would probably have to implement your own onscreen keyboard.