Receive iPhone keyboard events

iamMobile picture iamMobile · Aug 12, 2009 · Viewed 14k times · Source

Is there anyway to trap all keyboard events across my application? I need to know if user is entering anything using keyboard across my application (Application has multiple views). I was able to capture touchEvents by subclassing UIWindow but unable to capture keyboard events.

Answer

oxigen picture oxigen · Aug 13, 2009

Use NSNotificationCenter

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyPressed:) name: UITextFieldTextDidChangeNotification object: nil];

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyPressed:) name: UITextViewTextDidChangeNotification object: nil];

........

-(void) keyPressed: (NSNotification*) notification
{
  NSLog([[notification object]text]);
}