Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa: Learn currently selected keyboard layout / input language

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.

like image 602
Ilya Birman Avatar asked Apr 23 '09 13:04

Ilya Birman


People also ask

How to change spanish keyboard to english?

Go to Start > Control Panel > Clock, Language, and Region. 2. On the “Region and Language” option, click on “Change Keyboards or Other Input Methods.”

How to change keyboard language excel?

Click the language icon on the Language bar, which should appear on your task bar near where the clock is, and then click the language that you want to use. Keyboard shortcut: To switch between keyboard layouts, press Alt+Shift.

How to change keyboard language microsoft?

Open Region and Language by clicking the Start button , clicking Control Panel, clicking Clock, Language, and Region, and then clicking Region and Language. Click the Keyboards and Languages tab. Under Display language, choose a language from the list, and then click OK.


1 Answers

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.
like image 140
Nicholas Riley Avatar answered Oct 06 '22 20:10

Nicholas Riley