Search code examples
iphoneiphone-softkeyboard

Receive iPhone keyboard events


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.


Solution

  • 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]);
    }