Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

xcode - change input source language programmatically OSx

I'm having problem to change a textfield input language source through codes !
All I want is to change the keyboard language to specific language when ever I enter a textfield.

I'm already searched here and just found some small guidance for Iphone but I'm working on OSx App.

Thank you guys helping me again !

EDIT:
well ! new approaches : If I change my system preferences to appropriate language then most of the problems solved ! Date formatter become true and if I use this code snippet I can achieve correct language name ( before I just got en):

NSLocale * locale = [NSLocale currentLocale];        
NSString * localLanguage = [locale objectForKey:NSLocaleLanguageCode] ;
NSLog (@"Language : %@", localLanguage);    

BUT, I want to change the keyboard input language without changing the whole system preference. In fact, I want to change the Text Input service language to other installed language ( I have 2 ) when I enter into a special textfield ! ( is it clear ? )

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, I answered my own question AGAIN !

for whom that may visit this question :
to get reached the user's keyboard language Input sources you have to follow this instructions :

  1. TIS ( Text Input Service ) is related to Carbon framework. So, first of all you have to import carbon.h to your implementation file:

    #import <Carbon/Carbon.h>

  2. Add carbon framework to your framework resource. To do this you have to navigate to Mac OSx Application Target -> Linked Frameworks and Libraries -> add carbon frame work.

  3. to change the keyboard input source, you can use for example controlTextDidBeginEditing delegate to detect user's textfield selection.Then you can select the proper language from installed language sources. for example I have two language installed, en and fa so I have 2 keyboard layout in language bar. Then you can select the language by selecting it's index.

  4. to find your desired language index you can use this :
    NSArray *langs=[NSLocale preferredLanguages];
    langs included by languages that you can reached by it's indexes.

  5. Now, it's time to change text input source programmatically using this codes :
    NSArray*langsArray=(NSArray*)TISCreateInputSourceList(NULL,FALSE); //make a list of installed languages
    TISInputSourceRef faSource=(TISInputSourceRef)[langsArray objectAtIndex:1]; //my second language is farsi (persian)
    TISSelectInputSource(faSource); // now second language selected for keyboard input resource

Hope it helps you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...