Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the iPhone/iPad keyboard over a full screen OpenGL ES app

I'm working on an app that at some point requires keyboard input. My app uses OpenGL ES for display, and I have my own graphics framework that can display a text field, and knows how to manage the cursor and render text. This code works great on other platforms that have a physical keyboard, like Windows and OS X.

All I need to get the iOS version of my app working is to be able to bring the keyboard up and down programmatically, and also to get the key press events from the user into my view so that I can then route them into my framework's own event system.

I saw this question, but I could not make the solution depicted there work. Not sure if it is because I'm doing something wrong, or because it doesn't work on current iOS releases.

EDIT: It would be as helpful to be pointed at a working app with source code that creates any UI element programmatically and displays it on top of a GL ES screen. I think I can figure the rest out if I get that part understood.

like image 572
Miguel Avatar asked Aug 31 '11 06:08

Miguel


People also ask

How do I get the keyboard to appear on my iPad?

Go to Settings > Accessibility > Keyboards, tap Full Keyboard Access, then turn on Full Keyboard Access.

Why won't my keyboard appear on my iPad?

As is often the case with computers, it's possible that some sort of temporary software glitch is keeping the keyboard from appearing on screen. Restarting your iPad — turning it off and then back on again — can resolve most of these kinds of problems. Restart it and check the onscreen keyboard again.


1 Answers

I can't point you at a working example, but I believe what you're looking for is here http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKeyInput_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UIKeyInput

The basic idea is that you implement the UIKeyInput protocol on a UIResponder object, such as a UIView. Make the view the firstResponder and the keyboard should automatically appear. The UIKeyInput protocol gives you simple feedback for inserting a character and deleting a character when the user presses buttons on the keyboard.

This isn't working code, but it would look something like this:

@interface MyKeyboardView : UIView <UIKeyInput>
@end

@implementation MyKeyboardView
- (void)insertText:(NSString *)text {
    // Do something with the typed character
}
- (void)deleteBackward {
    // Handle the delete key
}
- (BOOL)hasText {
    // Return whether there's any text present
    return YES;
}
- (BOOL)canBecomeFirstResponder {
    return YES;
}
@end

when the view is attched, make it the first responder to show the keyboard

[myView becomeFirstResponder];

or resign first responder to dismiss the keyboard

[myView resignFirstResponder];

Edit: make sure your view is attached to the view hierarchy or this probably won't do anything.

like image 143
John Stephen Avatar answered Oct 05 '22 16:10

John Stephen