tags:

views:

46

answers:

1

Hi,

I need to display the system keyboard if any text entry fields are selected with in my app. I can't seem to find any resources on how to do this. Any tips?

Edit: I'm asking this question because I'm making a full screen application and need to show/hide based on user selection.

Thanks,
Teja

+1  A: 

The reason why you can't find the info is that it's a C API and it's not in Cocoa. The API you need to use is Text Input Sources Services. Try this sample:

#import <Foundation/Foundation.h>
#import <Carbon/Carbon.h>

int main(){
    NSAutoreleasePool*pool=[[NSAutoreleasePool alloc] init];
    NSDictionary*property=[NSDictionary dictionaryWithObject:(NSString*)kTISTypeKeyboardViewer
                                                      forKey:(NSString*)kTISPropertyInputSourceType];
    NSArray*tisArray=(NSArray*)TISCreateInputSourceList((CFDictionaryRef)property,NO);
    // tisArray usually only contains just one keyboard viewer
    // in a rare case the user might have another viewer installed, beware
    TISInputSourceRef keyboardViewer=(TISInputSourceRef)[tisArray objectAtIndex:0];
    TISSelectInputSource(keyboardViewer);
    CFRelease((CFTypeRef)tisArray);
    [pool drain];
    return 0;
}

It's a Core Foundation-type API, so if you know Cocoa the usage is similar; Core Foundation is basically a C API for NSObjects. Mostly you can use toll-free bridging, see this SO question. You need to be careful when you retain/release them in a garbage-collected environment. TIS was introduced in 10.5; I don't know why Apple didn't introduce it as a Cocoa API back then. Apparently they hadn't made up their mind to dispose of 64bit Carbon then.

Yuji
Thanks! [characterlimitblah]
Tejaswi Yerukalapudi
Hi, Could you help me out with one additional thing? The keyboard seems to come up fine, but when I launch it from an application that's already in full-screen mode, the keyboard seems to get pushed to the background instead of showing up "over" the application window. Any reason why this might be happening?
Tejaswi Yerukalapudi
What's the window level of your full-screen window? Set it below the keyboard's window level, using `setLevel:`.
Yuji
Oops, didn't see your reply and posted an additional question. Let me follow up on that - http://stackoverflow.com/questions/4026003/keyboard-in-a-fullscreen-cocoa-app
Tejaswi Yerukalapudi