1

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?

Kevin
  • 53,822
  • 15
  • 101
  • 132

3 Answers3

2

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.

Community
  • 1
  • 1
Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
1

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).

BarrettJ
  • 3,431
  • 2
  • 29
  • 26
0

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];
bogen
  • 9,954
  • 9
  • 50
  • 89
  • Thanks, but these events don't allow me to track how long a certain key is pressed. I am capturing what was pressed through shouldChangeCharactersInRange method already. – user1071490 Nov 29 '11 at 15:18