以编程方式更改输入源语言OSx [英] change input source language programmatically OSx

查看:109
本文介绍了以编程方式更改输入源语言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.

我已经在这里搜索过,只是找到了一些有关Iphone的小指南,但是我正在研究 OSx App .

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

谢谢你们再次帮助我!


出色地 !新方法: 如果我将系统偏好设置更改为适当的语言,那么大多数问题就解决了! 日期格式设置为true,如果我使用此代码段,我可以获得正确的语言名称(在我刚获得 zh_CN 之前):


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);    

但是,我想更改键盘输入语言,而不更改整个系统的偏好设置.实际上,当我输入特殊的文本字段时,我想将文本输入服务语言更改为其他已安装语言(我有2种)! (清楚吗?)

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 ? )

推荐答案

好吧,我再次回答了自己的问题!

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 (文本输入服务)与 Carbon框架有关.因此,首先您必须将 carbon.h 导入到您的实施文件中:

  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>

添加碳框架到您的框架资源中.为此,您必须导航到 Mac OSx应用程序目标->链接框架和库->添加碳框架.

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.

要更改键盘输入源,您可以使用例如controlTextDidBeginEditing委托来检测用户的textfield选择.然后,您可以从已安装的语言源中选择正确的语言.例如,我安装了两种语言 en fa ,因此我在语言栏中有2种键盘布局.然后,您可以通过选择索引来选择语言.

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.

要找到所需的语言索引,可以使用此方法:
NSArray *langs=[NSLocale preferredLanguages];
语言包含在其索引可以访问的语言中.

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.

现在是时候使用此代码以编程方式更改文本输入源了:
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

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

希望它对您有帮助.

这篇关于以编程方式更改输入源语言OSx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆