6

Is it possible and how to reliably gather the current keyboard language and/or layout via command-line in Ubuntu?

We've already tried different options:

  1. localectl (status);
  2. cat /etc/default/locale;
  3. cat /etc/default/keyboard;
  4. setxkbmap -query;
  5. gsettings ...;
  6. setxkbmap -print | grep xkb_symbols (setxkbmap -v | awk -F "+" '/symbols/ {print $2}');
  7. Tried to install xkblayout-state, but make failed on fresh OS install - skipped.

All of these output the same per each command with keyboard layout change - us layout.

uname -a shows Linux x4 4.4.0-97-generic #120-Ubuntu SMP Tue Sep 19 17:28:18 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

The current distribution is KDE Neon 5.8 (based on Ubuntu) and window system is X11.


The outputs of the above commands:

$ localectl status:

System Locale: LANG=en_US.UTF-8
   LC_NUMERIC=lt_LT.UTF-8
   LC_TIME=lt_LT.UTF-8
   LC_MONETARY=lt_LT.UTF-8
   LC_PAPER=lt_LT.UTF-8
   LC_NAME=lt_LT.UTF-8
   LC_ADDRESS=lt_LT.UTF-8
   LC_TELEPHONE=lt_LT.UTF-8
   LC_MEASUREMENT=lt_LT.UTF-8
   LC_IDENTIFICATION=lt_LT.UTF-8
 VC Keymap: n/a
X11 Layout: us
X11 Model: pc105

$ cat /etc/default/locale:

#  File generated by update-locale                                                                                                                                                 
LANG="en_US.UTF-8"                                                                                                                                                                 
LC_NUMERIC="lt_LT.UTF-8"                                                                                                                                                           
LC_TIME="lt_LT.UTF-8"                                                                                                                                                              
LC_MONETARY="lt_LT.UTF-8"                                                                                                                                                          
LC_PAPER="lt_LT.UTF-8"                                                                                                                                                             
LC_NAME="lt_LT.UTF-8"                                                                                                                                                              
LC_ADDRESS="lt_LT.UTF-8"                                                                                                                                                           
LC_TELEPHONE="lt_LT.UTF-8"                                                                                                                                                         
LC_MEASUREMENT="lt_LT.UTF-8"                                                                                                                                                       
LC_IDENTIFICATION="lt_LT.UTF-8"

$ cat /etc/default/keyboard:

# KEYBOARD CONFIGURATION FILE

Consult the keyboard(5) manual page.

XKBMODEL="pc105" XKBLAYOUT="us" XKBVARIANT="" XKBOPTIONS=""

BACKSPACE="guess"

$ setxkbmap -query:

rules:      evdev
model:      pc101
layout:     us,lt
options:    grp:alt_shift_toggle

$ setxkbmap -print | grep xkb_symbols:

        xkb_symbols   { include "pc+us+lt:2+inet(evdev)+group(alt_shift_toggle)"        };

$ setxkbmap -v | awk -F "+" '/symbols/ {print $2}':

us

$ cat ~/.config/kxkbrc:

[Layout]
DisplayNames=,
LayoutList=us,lt
LayoutLoopCount=-1
Model=pc101
Options=grp:alt_shift_toggle
ResetOldOptions=true
ShowFlag=false
ShowLabel=true
ShowLayoutIndicator=true
ShowSingle=false
SwitchMode=Global
Use=true

All of these commands were executed two times (before and after keyboard language/layout change) - no useful enough output.


However:

$ xset -q | grep -A 0 'LED' | cut -c59-67 (English language is enabled):

00000000

$ xset -q | grep -A 0 'LED' | cut -c59-67 (Lithuanian language is enabled):

00001000

Considering it's changing, is it a correct and reliable option?

Artfaith
  • 219
  • 1
  • 3
  • 10

1 Answers1

7

As regards Ubuntu 17.10, I just figured out that the current keyboard layout in a desktop session is saved differently.

I have a Swedish and English (US) layout available. Previously I could do:

$ gsettings get org.gnome.desktop.input-sources sources
[('xkb', 'se'), ('xkb', 'us')]
$ gsettings get org.gnome.desktop.input-sources current
uint32 0

The current value 0 (zero) told me that the current layout was the first layout in the sources list. However, in 17.10 the current value is not altered if I switch layout.

Instead, 17.10 includes the mru-sources key, which lists the most recently used input sources. Hence, when Swedish is my current layout, it looks like this:

$ gsettings get org.gnome.desktop.input-sources mru-sources
[('xkb', 'se'), ('xkb', 'us')]

and if I switch to English it looks like this:

$ gsettings get org.gnome.desktop.input-sources mru-sources
[('xkb', 'us'), ('xkb', 'se')]

So the current layout is simply the first input source in the mru-sources list.

Gunnar Hjalmarsson
  • 33,540
  • 3
  • 64
  • 94