Show iPhone soft keyboard even though a hardware keyboard is connected
Asked Answered
P

5

24

My iPad app uses an external "device" that acts as a hardware keyboard. But, at some point in the settings, I need to input text and I can't use the "device" ("device" is not a keyboard). So, is there any way to force pop the soft keyboard even thought I have a hardware keyboard connected?

Pythagoras answered 24/7, 2010 at 17:49 Comment(0)
C
22

Yes. We've done this in a few of our apps for when the user has a Bluetooth scanner "keyboard" paired with the device. What you can do is make sure your textField has an inputAccessoryView and then force the frame of the inputAccessoryView yourself. This will cause the keyboard to display on screen.

We added the following two functions to our AppDelegate. The 'inputAccessoryView' variable is a UIView* we have declared in our app delegate:

//This function responds to all textFieldBegan editing
// we need to add an accessory view and use that to force the keyboards frame
// this way the keyboard appears when the scanner is attached
-(void) textFieldBegan: (NSNotification *) theNotification
{
    UITextField *theTextField = [theNotification object];
    //  NSLog(@"textFieldBegan: %@", theTextField);

    if (!inputAccessoryView) {
        inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, navigationController.view.frame.size.width, 1)];
    }

    theTextField.inputAccessoryView = inputAccessoryView;

    [self performSelector:@selector(forceKeyboard) withObject:nil afterDelay:0];
}

//Change the inputAccessoryView frame - this is correct for portrait, use a different
// frame for landscape
-(void) forceKeyboard
{
    inputAccessoryView.superview.frame = CGRectMake(0, 759, 768, 265);
}

Then in our applicationDidFinishLaunching we added this notification observer so we would get an event anytime a text field began editing

    //Setup the textFieldNotifications
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBegan:) name:UITextFieldTextDidBeginEditingNotification object:nil];

Hope that helps!

Cassandra answered 1/10, 2010 at 7:5 Comment(9)
Hi Brian, first of all thank you very much for your solution. Unfortunately I encountered the problem of the keyboard freezing or the whole app crashing after calling the keyboard for a couple of times like that. Did you have the same problem, or even better a solution? :-) ThanksWalter
No we haven't seen that happen at all. We've used this code in dozens of apps that have had extensive trade show use, and thus far nobody has mentioned that type of issue.Cassandra
Despite my best efforts I kept on running into the same issue - must be some interdependency with some other code of mine, as the code worked perfect in a different app. Again, thanks for your effort!Walter
Thanks a lot for the solution ! I wanted that in a UIWebView. I just had to search in the subviews of the UIWebView for the first responder, and replace the frame of its inputAccessoryView's superview and it worked ! :)Gsuit
Got this working in a webview. But how can i hide the keyboard when the hide keyboard button is pressed (iPad..).. UIKeyboardWillHideNotification is never called..Undesigned
I think this solution, without using any private APIs or spelunking the depths of a view chain hierarchy to find some obscurely named keyboard view object, is about as good as I have seen. Nicely done.Glassco
Brain, help me on this. textFieldBegan method is called but the keyboard is not visibleGeisler
@BrianRobbins Can you help me with this for iOS 8 and above versions ? I have implemented the same solution but the frame of the superview (Keyboard) is not getting changed. May be due to autolayout or something.Sorosis
I'm also not having any success getting this to work on iOS 10. I can get a reference to the inputAccessoryView's superview, which is a UIInputSetHostView, but setting its frame seems to have no effect.Etamine
P
2

The solutions here didn't work on iOS 13 or aren't App Store compatible so I solved the problem by creating my own soft keyboard. It is pretty basic but works. Feel free to contribute!

Project on Github

All you have to do is add SoftKeyboardView.swift to your project and somewhere (e.g. appDidFinishLaunching) hit the singleton:

Usage:

SoftKeyboardManager.shared.disabled = false
Panaggio answered 24/12, 2019 at 0:7 Comment(2)
How to use this project?Deprecative
Thanks for asking, @YonathanGoriachnick. I just updated the answer and the project with a brief explanation of how to usePanaggio
A
1

There’s no way to do this with the current SDK. Please let Apple know via the Bug Reporter.

Aldarcy answered 28/9, 2010 at 19:29 Comment(0)
Y
0

Since I have the same problem, the closest solution I have found is to use Erica Sadun's app called KeysPlease which is available via cydia and modmyi. It's description is "Use soft kb even when connected to a BT kb.".

Additionally I have found that if you have a physical keyboard also attached, in my case via the iPad keyboard doc, you can bring up the keyboard using a key which seems to map to the eject key on a bluetooth keyboard. Perhaps there is a way to inject this key as if it was pressed on an attached keyboard?

I really wish there was a more official coding solution to this.

Yaakov answered 12/8, 2010 at 22:30 Comment(0)
Q
0

When my app connect bluetooth device, keyboard wouldn't show.I try set force the frame of the inputAccessoryView as Brian Robbins say. It didn't work.

Then I use a stupid way to solve.I found when I click textfield or textview one more time, keyboard will show. So I just need to simulate touch in textfield or textview once , it works.

If you want to do some simulate touch, check this. https://github.com/HUYU2048/PTFakeTouch

Quintana answered 5/3, 2018 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.