Cocoa:了解当前选择的键盘布局/输入语言

Ily*_*man 5 layout cocoa input

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

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

[NSInputManager currentInputManager]
Run Code Online (Sandbox Code Playgroud)

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

[[NSInputManager currentInputManager] localizedInputManagerName]
Run Code Online (Sandbox Code Playgroud)

对我来说,最好只取回EN或FR所用语言的双字母缩写,但菜单栏中显示的键盘布局名称也可以使用.

有任何想法吗?谢谢.

编辑:我还发现AppleSelectedInputSourcesChangedNotification被发布到

[NSDistributedNotificationCenter defaultCenter]
Run Code Online (Sandbox Code Playgroud)

当用户更改布局时,不会将有关新选择布局的信息"附加"到此通知.

Nic*_*ley 13

键盘布局到语言组合通常是一对多的,因此虽然您可以获得当前所选键盘布局的本地化名称(或者更常见的是输入源),但源可用于键入多种语言的文本.你为什么要这样做?

也就是说,您可以使用文本输入源服务获取有关当前文本输入源的信息.例如:

  TISInputSourceRef source = TISCopyCurrentKeyboardInputSource();
  NSLog(@"languages: %@",
        TISGetInputSourceProperty(source, kTISPropertyInputSourceLanguages));
  NSLog(@"localized name: %@",
        TISGetInputSourceProperty(source, kTISPropertyLocalizedName));
Run Code Online (Sandbox Code Playgroud)

给我:

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.
Run Code Online (Sandbox Code Playgroud)