11

In my C/C++ program I want to know which language is user going to type. I mean the language whose ID is displayed in the corner of the task bar. Like EN or RU or ZH or FR or IT.

I know how to get the list of possible layout:

$ setxkbmap -query | grep layout

output:

layout:     us,ru

But how to know which one is selected right now? (for the current window)

exebook
  • 32,014
  • 33
  • 141
  • 226
  • 2
    There is a risk that there is no common standard for this. Did you read: http://nabble.documentfoundation.org/Detecting-current-Keyboard-layout-in-English-td4049780.html – grebneke Jan 31 '14 at 15:27
  • Or maybe this: http://stackoverflow.com/questions/7021111/kde-how-do-i-find-and-switch-current-globar-keyboard-layout-from-cli – grebneke Jan 31 '14 at 15:28

1 Answers1

10

setxkbmap -print isn't helpful in this case, which was also my first idea. I have found a little tool, very easy to compile

sudo apt-get install git
mkdir -p `~/src`
cd `~/src`
git clone https://github.com/nonpop/xkblayout-state.git 
cd xkblayout-state
make

Now you can run the command ./xkblayout-state to get the current layout, e.g.

./xkblayout-state print "%n"                          
German%

or list all installed layouts

./xkblayout-state print "%N"
German
English
English

In this case without a trailing %. I would have expected that, because I have not added a \n.

A.B.
  • 460
  • 5
  • 18
  • works for me, but I noticed it only shows "system" (RU,EN in my case) languages, ignoring ibus Chinese. – exebook Nov 13 '15 at 11:04
  • yes, `print "%s %n"` shows `ru Russian` for Russian, `en English` for English, and `en English` for Chinese. – exebook Nov 13 '15 at 11:51