Tejaswi Yerukalapudi
Tejaswi Yerukalapudi

Reputation: 9157

Display System Keyboard Mac application?

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

Upvotes: 0

Views: 817

Answers (1)

Yuji
Yuji

Reputation: 34185

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.

Upvotes: 2

Related Questions