views:

976

answers:

2

The iPad virtual keyboard will disappear in one of (at least) these 3 circumstances:

  1. If the control (say, a UITextField) programmatically resigns first responder.
  2. If the user taps the "dismiss keyboard" button in the lower right.
  3. If the user connects to the USB/keyboard dock peripheral.

In all cases, I get the UIKeyboardWillHideNotification.

The problem is that the first two cases are generally equivalent-- in other words, the user is done editing the text field. But in the third case, the text field is still being edited, just from another input source.

The problem is detecting the difference between cases 2 and 3. All I get in both cases is UIKeyboardWillHideNotification. In case 2, I generally want to also lock the edit control and commit the value. In case 3, I generally want to do nothing and allow editing to continue.

But how can I tell the difference?

Apple's Pages app seems to be able to distinguish this on document-title renaming.

+1  A: 

I would look at the UIKeyboardBoundsUserInfoKey passed with the notification. The physical keyboard probably has empty bounds.

drawnonward
Unfortunately no. The physical keyboard doesn't generate a UIKeyboardWillShowNotification when it appears. Attaching it merely generates a WillHide notification that corresponds to the virtual one.
quixoto
In case 1, the control loses focus. Is the same true in case 2? I assume in case 3 the control retains focus.
drawnonward
In cases 2 and 3, the control continues to be firstResponder. Resigning first responder will dismiss the keyboard, but the opposite is not apparently true.
quixoto
+2  A: 

It sounds like you're just trying to figure out when a user is done editing. You could listen for the UITextFieldTextDidEndEditingNotification notification for cases 1 and 2.

A much simpler solution would be couldn't you just check against the editing property of UITextField to determine if it's still supposed to be editing or not? I don't have a physical keyboard, so I have no way to test this. I'm just guessing.

Here's a link to the documentation on that property: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITextField_Class/Reference/UITextField.html#//apple_ref/occ/instp/UITextField/editing

I'm very curious to know if this works or not... :)

lewiguez
For case #2, the text field is still editing, and it's still the first responder. There's just no keyboard.
quixoto