可可:学习当前选择的键盘布局/输入语言 [英] Cocoa: Learn currently selected keyboard layout / input language

查看:208
本文介绍了可可:学习当前选择的键盘布局/输入语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何学习当前选择的键盘布局/输入语言?



我在玩NSInputManager,但无法实现任何目标。

  [NSInputManager currentInputManager] 

返回(null)(与%@一起报告),因此

  [[NSInputManager currentInputManager] localizedInputManagerName] / code> 

这是我最好的回答一个两个字母缩写的语言使用像EN或FR,但



任何想法?感谢。



编辑:我还发现AppleSelectedInputSourcesChangedNotification发布到



<$当用户更改布局时,p $ p> [NSDistributedNotificationCenter defaultCenter]

解决方案

键盘布局与语言组合通常是一对一的 - 许多,因此,虽然您可以获取当前选定的键盘布局(或更一般地,输入源)的本地化名称,但源可用于以多种语言键入文本。为什么要这样做?



也就是说,您可以使用文本输入源服务。例如:

  TISInputSourceRef source = TISCopyCurrentKeyboardInputSource(); 
NSLog(@languages:%@,
TISGetInputSourceProperty(source,kTISPropertyInputSourceLanguages));
NSLog(@localized name:%@,
TISGetInputSourceProperty(source,kTISPropertyLocalizedName));

给我:

  2009-04-23 14:30:17.581示例[30688:10b] languages:(
en,
ca,
da,
de,
es,
eu,
fr,
ga,
gl,
gv,
id,
it,
kw,
ms,
nb,
nl,
nn,
om,
pt,
so,
sq ,
sv,
sw

2009-04-23 14:30:17.584 sample [30688:10b]本地化名称:US


How do I learn currently selected keyboard layout / input language?

I was playing with NSInputManager but wasn’t able to achieve anything.

[NSInputManager currentInputManager]

returns (null) (as reported with %@) and thus

[[NSInputManager currentInputManager] localizedInputManagerName]

It would be the best for me to just get back a two-letter abbreviation of the language used like EN or FR, but the keyboard layout name as displayed in menubar will also work.

Any ideas? Thanks.

Edit: I also found out that an AppleSelectedInputSourcesChangedNotification gets posted into

[NSDistributedNotificationCenter defaultCenter]

when user changes the layout, however no information about the newly selected layout is "attached" to this notification.

解决方案

Keyboard layout to language combinations are typically one-to-many, so while you can get the localized name of the currently selected keyboard layout (or, more generally, the input source), the source can be used to type text in many languages. Why do you want to do this?

That said, you can get information about the current text input source using Text Input Source Services. For example:

  TISInputSourceRef source = TISCopyCurrentKeyboardInputSource();
  NSLog(@"languages: %@",
        TISGetInputSourceProperty(source, kTISPropertyInputSourceLanguages));
  NSLog(@"localized name: %@",
        TISGetInputSourceProperty(source, kTISPropertyLocalizedName));

gives me:

2009-04-23 14:30:17.581 sample[30688:10b] languages: (
    en,
    ca,
    da,
    de,
    es,
    eu,
    fr,
    ga,
    gl,
    gv,
    id,
    it,
    kw,
    ms,
    nb,
    nl,
    nn,
    om,
    pt,
    so,
    sq,
    sv,
    sw
)
2009-04-23 14:30:17.584 sample[30688:10b] localized name: U.S.

这篇关于可可:学习当前选择的键盘布局/输入语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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