Reputation: 1
How can I monitor the keyboard and determine KeyPress events ?
Upvotes: 0
Views: 2096
Reputation: 1386
The ShouldChangeCharacters of UITextField is called BEFORE the text is changed and it is not the same as the Changed event of UITextView. This means that getting the text on the ShouldChangeCharacters or ShouldChangeText method, will allways return the text BEFORE the last key was pressed.
The UITextView has a Changed event like the KeyPress event. UITextField has not such event yet.
A workaround for the UITextField can be found here: bugzilla.xamarin.com/show_bug.cgi?id=864
NSNotificationCenter.DefaultCenter.AddObserver
(UITextField.TextFieldTextDidChangeNotification, (notification) => { Console.WriteLine ("Character received! {0}", notification.Object == TextField); });
Upvotes: 1
Reputation: 1779
If you want to monitor key press events for UITextView, then override the UITextViewDelegate
's ShouldChangeText()
method.
If you want to monitor key press events for UITextField, there's a similar method in UITextFieldDelegate
called ShouldChangeCharacters()
.
Find more info on the usage of these methods here and here.
Upvotes: 2