Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get current active keyboard layout(language) in linux

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)

like image 572
exebook Avatar asked Jan 31 '14 14:01

exebook


People also ask

How do I get the current keyboard layout in Linux?

You can check keyboard file for keyboard layout information... Value of XKBLAYOUT is the layout of keyboard. Change it to another possible value and reboot the machine to take effects.

How do I find my current keyboard layout?

Steps to preview keyboard layout in Windows 10:Step 1: Access Control Panel. Step 2: Select Change input methods. Step 3: Click Options to open Language options. Step 4: Click Add an input method.

How do I know what keyboard layout I have Ubuntu?

In the Ubuntu Desktop, click System Settings. Click Keyboard Layout. The Keyboard Layout window opens. Click the plus (+) sign in the bottom-left corner to open the available keyboard layouts.


1 Answers

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.

like image 194
A.B. Avatar answered Oct 21 '22 18:10

A.B.