How do I know if there's a hardware keyboard?
cocoa-touch, ios, keyboard, objective-c
Solution
Input accessory views are shown at the bottom of the screen if there's a hardware keyboard attached (otherwise they're attached to the top of the on-screen keyboard).
Set the accessory view of your input view to an empty `UIView` of size `CGRectZero`. You will receive the show / hide notifications for both cases, and be able to determine the available screen size from the metrics in the notification's userinfo dictionary.
UIView *inputAccessoryView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
inputAccessoryView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
textView.inputAccessoryView = inputAccessoryView;
…
- (void)keyboardWillShow:(NSNotification*)aNotification {
NSLog(@"%@", [aNotification userInfo]);
}
Logged metrics - note the frame in `UIKeyboardFrameEndUserInfoKey` is off screen
// (NSDictionary *) {
// UIKeyboardAnimationCurveUserInfoKey = 0;
// UIKeyboardAnimationDurationUserInfoKey = "0.25";
// UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {768, 264}}";
// UIKeyboardCenterBeginUserInfoKey = "NSPoint: {384, 891}";
// UIKeyboardCenterEndUserInfoKey = "NSPoint: {384, 1155}";
// UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 758}, {768, 264}}";
// UIKeyboardFrameChangedByUserInteraction = 0;
// UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 1022}, {768, 264}}";
// }
Update - very similar to user721239's answer in the related question.
Problem
Possible Duplicate: How can I detect if an external keyboard is present on an iPad? When a user begins editing a textfield in my iPhone app's UI, I want to use the space between the textfield and the keyboard for a list of related options. Using the regular (on-screen) keyboard from iOS, there are a couple of convenient notifications to listen to, that help me decide how much space I have left for the list in between. However when you attach (or simulate the use of) a hardware keyboard, these notifications are not posted anymore. Not too unexpected, but this leaves me with a bit of a challenge. If the user uses the hardware keyboard, I have more space for the list. But how can I determine whether the user has a hardware keyboard or not, if there's no triggered notification or delegate method to listen to? Or is there? What's also to be considered is that a user can first use the app with the software keyboard and then attach his/her hardware keyboard and continue working with the app. At that point, the software keyboard will hide, but the user is still editing the textfield. I found one old dupe for this question, but it is unanswered: - How do I detect that a hardware keyboard is attached to an iPhone?