How can I detect if an external keyboard is present on an iPad?

ghz 1years ago ⋅ 2399 views

Question

Is there a way to detect if an external (bluetooth or usb) keyboard is connected to the iPad?


Answer

An indirect and SDK-safe way is to make a text field a first responder. If the external keyboard is present, the UIKeyboardWillShowNotification local notification shall not be posted.

Update: This is no longer true since iOS 9, however you may use the keyboard dimensions to determine if a hardware or software keyboard is involved. See [How to reliably detect if an external keyboard is connected on iOS 9?](https://stackoverflow.com/questions/31991873/how-to-reliably- detect-if-an-external-keyboard-is-connected-on-ios-9) for details.

You can listen to the "GSEventHardwareKeyboardAttached" (kGSEventHardwareKeyboardAvailabilityChangedNotification) Darwin notification, but this is a private API, so it's possible your app will get rejected if you use this. To check if the external hardware is present, use the private GSEventIsHardwareKeyboardAttached() function.

UIKit listens to this and sets the UIKeyboardImpl.isInHardwareKeyboardMode property accordingly, but again this is private API.