views:

2146

answers:

2

Is there a way to programatically enable and disable return key on the UIKeyboard? The closest I could find is enablesReturnKeyAutomatically, but that only will tell whether to disable it at all.

+2  A: 

If you can get the UIKeyboard object itself (something not exposed in the SDK, mind you, so Apple may not be happy if you use these calls), then there's a convenient setReturnKeyEnabled: member function.

id keyboard = [self magicallyGetAUIKeyboardInstance];
[keyboard setReturnKeyEnabled: NO];

(via Erica Sadun's dump of the 2.2 iPhone frameworks)

The implementation of magicallyGetAUIKeyboardInstance is described here.

Elliot Kroo
Haven't got a chance to test it yet, but shouldn't just [UIKeyboard activeKeyboard] work?
Ben Alpert
A: 

One good idea is to create one file to access this class from anywhere. Here is the code:

UIKeyboard.h

#import <UIKit/UIKit.h> 

@interface UIApplication (KeyboardView)

    - (UIView *)keyboardView; 

@end

UIKeyboard.m

#import "UIKeyboard.h"

@implementation UIApplication (KeyboardView)

- (UIView *)keyboardView
{
    NSArray *windows = [self windows];
    for (UIWindow *window in [windows reverseObjectEnumerator])
    {
     for (UIView *view in [window subviews])
     {
      if (!strcmp(object_getClassName(view), "UIKeyboard"))
      {
       return view;
      }
     }
    }

    return nil;
}

@end

Now you can import and access this class from your own class:

#import "UIKeyboard.h"

    // Keyboard Instance Pointer.
    UIView *keyboardView = [[UIApplication sharedApplication] keyboardView];

A full documentation of this class you cand find here: http://ericasadun.com/iPhoneDocs/_u_i_keyboard_8h-source.html

More information you cand find here: http://cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html

SEQOY Development Team