13

I want to detect whenever the user presses any keyboard key.

Any method which is called only on typing any character and not when keyboard is shown.

Thanks!!

AtWork
  • 1,283
  • 1
  • 14
  • 34

2 Answers2

40

You can directly handle keyboard events every-time a user presses a key:

Swift

For Textfield use following delegate method -

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

}

For TextView use following delegate method -

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

}

Objective C

In case of UITextField

- (BOOL)textField:(UITextField *)textField
          shouldChangeCharactersInRange:(NSRange)range
          replacementString:(NSString *)string {

    // Do something here...
}

In Case of UITextView :

- (BOOL)textView:(UITextView *)textView
      shouldChangeTextInRange:(NSRange)range 
      replacementText:(NSString *)text {

    // Do something here...
}

So every-time one of these method is called for each key you press using keyboard.

You can use NSNotificationCenter also. You only need do add any of these in ViewDidLoad method.

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

UITextField :

[notificationCenter addObserver:self
                       selector:@selector(textFieldText:)
                           name:UITextFieldTextDidChangeNotification
                         object:yourtextfield];

Then you can put your code in method textFieldText::

- (void)textFieldText:(id)notification {

    // Do something here...
}

UITextView

[notificationCenter addObserver:self
                       selector:@selector(textViewText:)
                           name:UITextViewTextDidChangeNotification
                         object:yourtextView];

Then you can put your code in method textViewText::

- (void)textViewText:(id)notification {

    // Do something here...
}

Hope it helps .

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
  • 1
    If I have a UITextView and I press the ReturnKey, the method textViewDidChange will also be called. Is there a way to recognize which key has been pressed on the keyboard? – Alex Cio Aug 29 '13 at 10:16
  • What about for key presses in a UIWebView? – Rob Caraway Apr 16 '14 at 21:04
  • 6
    Note: shouldChangeTextInRange: doesn't get called if the text field is empty and you press the delete key. Text field seems to detect that the field hasn't changed so the delegate method doesn't need to be fired = / – Zhang Apr 17 '14 at 03:02
  • what value is sent if the user presses the shift key? – rolling_codes Aug 02 '14 at 21:33
  • There is no shift key in iPhone keyboard. – Nishant Tyagi Aug 03 '14 at 03:36
  • @NishantTyagi, I'm using the same event and handling tab key ('\t'). It works fine from the simulator using Hardware Keyboard, but it does not work with device using external blue tooth keyboard, even the delegate is not fired. Do I need to handle differently for external blue tooth keyboard? – Ali Raza Jan 29 '16 at 07:48
  • But how should you handle shouldChangeTextInRange boolians correctly so that if the user is actually typing or deleting the text, it would return Yes and if only the keyboard is up and theyre not typing it would return NO ? – Reza.Ab May 14 '18 at 22:53
1

Assuming the keyboard is showing as a result of the user tapping on a UITextfield, you can make yourself the delegate and implement this method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

It will be called every time a user presses a key.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118