19

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.

Community
  • 1
  • 1
Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • You couldn't make it work in what sense? The keyboard doesn't appear, you can't redirect text input (which would be better done with a delegate, by the way) or something else? Can you show your UITextField-related code? – Tommy Aug 31 '11 at 07:16
  • The problem is that the keyboard does not appear, correct. If I make the UITextField visible instead of hidden I also don't see it over my GL screen. – Miguel Grinberg Aug 31 '11 at 16:10
  • What graphics framework are you using to display the text field? – Paul de Lange Apr 28 '13 at 14:22
  • @PauldeLange: I use my own little framework on top of OpenGLES. I instantiate the UI components directly. – Miguel Grinberg Apr 28 '13 at 18:25

1 Answers1

22

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.

John Stephen
  • 7,625
  • 2
  • 31
  • 45
  • Thanks, I'll give this a try. Since I have a single view in my app I assumed it was already the first responder. I was caling becomeFirstResponder on the text field, which was a child of the view. Is there a benefit in creating a second view just for this? – Miguel Grinberg Sep 01 '11 at 03:49
  • 3
    Thank you so much for your help. I have managed to get the keyboard to pop up following your advice. One detail that you missed that may not be obvious (at least it wasn't for me) is that a UIView by default will not become first responder, you have to implement canBecomeFirstResponder and return YES from it to enable that. – Miguel Grinberg Sep 01 '11 at 06:10
  • I'm not sure if there's a significant benefit to using a second view to handle the keyboard, but perhaps if you were trying to process other events in the same view (touch events, perhaps) then using a child view could give you better control over the responder chain. – John Stephen Sep 02 '11 at 22:31
  • I'll modify the snippet to include the canBecomeFirstResponder message - thanks! – John Stephen Sep 02 '11 at 22:31
  • Holy cow, you just made it so easy.. this __is__ all you have to do! – bobobobo Apr 07 '13 at 02:38
  • what is myView ? – NovusMobile Oct 30 '17 at 05:16
  • @NovusMobile It’s an instance of MyKeyboardView, or any view implementing the protocol for UIKeyInput – John Stephen Oct 31 '17 at 13:31