Is there a way to detect if an external (bluetooth or usb) keyboard is connected to the iPad?
Control iPad with an external keyboardGo to Settings > Accessibility > Keyboards, tap Full Keyboard Access, then turn on Full Keyboard Access.
These portable full-size keyboards (sold separately) allow you to enter text while viewing the entire iPad screen.
Tap on Settings > General > Reset > Reset All Settings The Reset All followed by the reboot action should fix this issue for you, and your external smart keyboard should not have any problems connecting to your iPad running iPadOS.
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? 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.
There is another level to this.
Fortunately, there is enough information in the event to figure out whether the kbd will be presented, though it's still a little involved.
If we examine the notification dictionary we see this information:
UIKeyboardFrameBeginUserInfoKey = NSRect: {{0, 1024}, {768, 308}} UIKeyboardFrameEndUserInfoKey = NSRect: {{0, 980}, {768, 308}}
That was in Portrait; if we rotate the device to PortraitUpsideDown we get:
UIKeyboardFrameBeginUserInfoKey = NSRect: {{0, -308}, {768, 308}} UIKeyboardFrameEndUserInfoKey = NSRect: {{0, -264}, {768, 308}}
Similarly in LandscapeLeft and LandscapeRight we get different start and end locations.
Hmm... what do these numbers mean? You can see that the kbd is offscreen to start, but it does move a little. To make things worse, depending on the device orientation, the kbd locations are different.
However, we do have enough information to figure out what's going on:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With