Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField - detect input on built-in iPhone keyboard?

[UITextFieldVariable becomeFirstResponder]; is bringing the keyboard on to the screen.

How can I detect when a user types in a letter on the keyboard? I'd like to know each time a key has been pressed.

Thanks so much in advance!

like image 452
RanLearns Avatar asked Jan 17 '23 19:01

RanLearns


1 Answers

Use the NSNotificationCenter and take a look at the UITextFieldTextDidChangeNotification.

[[NSNotificationCenter defaultCenter] addObserver:self
          selector:@selector(textFieldChanged)
              name:UITextFieldTextDidChangeNotification
            object:textField];

Now implement a function -(void)textFieldChanged; and each time this function gets called compare the new text in the UITextField with the old text. Whatever has changed is the button that was pressed.

Do something similar for UITextFieldTextDidBeginEditingNotification and UITextFieldTextDidEndEditingNotification to ensure that buttons were pressed even though no text was typed (like pressing backslash).

like image 76
Dimme Avatar answered Feb 04 '23 08:02

Dimme