How to Programmatically Check Whether a Keyboard Is Present in iOS App

How to detect when keyboard is shown and hidden

In the ViewDidLoad method of your class set up to listen for messages about the keyboard:

// Listen for keyboard appearances and disappearances
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];

Then in the methods you specify (in this case keyboardDidShow and keyboardDidHide) you can do something about it:

- (void)keyboardDidShow: (NSNotification *) notif{
// Do something here
}

- (void)keyboardDidHide: (NSNotification *) notif{
// Do something here
}

Programmatically check if iPhone keyboard is on screen or not

Basically, you want to add an NSNotificationCenter observer:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow) name:UIKeyboardDidShowNotification object:nil];

How to programaticly check with swift if keyboard is visible in ios app

There are UIKeyboardWillShowNotification and UIKeyboardWillHideNotification notifications of NSNotificationCenter. Just subscribe to them

How to detect whether custom keyboard is activated from the keyboard's container app?

Here is a method I have used in one of my projects. I think it is what you asked for, hope it helps you.

- (BOOL)isCustomKeyboardEnabled {
NSString *bundleID = @"com.company.app.customkeyboard"; // Replace this string with your custom keyboard's bundle ID
NSArray *keyboards = [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] objectForKey:@"AppleKeyboards"]; // Array of all active keyboards
for (NSString *keyboard in keyboards) {
if ([keyboard isEqualToString:bundleID])
return YES;
}

return NO;
}

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

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.

Within an iOS app how do I detect that the user has pressed one of the arrow keys on their external keyboard?

Not actually knowing, I suspect the answer is "you can't, not in an official way".

Since a user-friendly UI is top priority for the iPad platform according to Apple, I think they are doing what they can to discourage behavior that would depend on a keyboard being available. Apps should behave the same and provide the same functionality with or without a hardware keyboard. Things like keyboard hotkeys or navigation that doesn't use on-screen elements are probably no-no:s for that reason.

But, like I said, this is just speculation.



Related Topics



Leave a reply



Submit