Question

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?

Was it helpful?

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.

OTHER TIPS

I don't believe you will be able to do this (esp. in an App Store app) as the system itself has extra menus it displays when holding down on certain keys and there may not even be an on screen keyboard (bluetooth keyboard).

Add these to listeners to your TextField

// editing began:
[textField addTarget:self action:@selector(textFieldEditingDidBegin:) forControlEvents:UIControlEventEditingDidBegin];
// editing changed:
[textField addTarget:self action:@selector(editingChanged:)forControlEvents:UIControlEventEditingChanged];
// editing ended: 
[textField addTarget:self action:@selector(editingDidEnd:) forControlEvents:UIControlEventEditingDidEnd];

You can also use these notifications that knows when the keyboard shows and hides:

// keyboard shows on screen:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow) name:UIKeyboardDidShowNotification object:nil];
// keyboard is hidden:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide) name:UIKeyboardDidHideNotification object:nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top