9

How to get current keyboard layout in command line on all distributions ? Is it possible ?

E.g. xset -q | grep LED | awk '{ print $10; }' shows different output with different keyboard layouts locally, but I've asked other people and I've noticed that for them it shows the same output even if their layout was changed. Is it possible, or did they do something wrong? Is there a clear/proper way to get the current exact keyboard layout?

I've started topic here, but it's clear that the solution depends on the distribution. There you can see that I've tried a lot of variants of getting this, but only xset worked.

By the way, what command would you use for each distribution? For example, in a script which would execute it to get the current keyboard layout depending on the distribution. Or, which command will output CURRENT toggled layout for Fedora, for KDE Neon, Ubuntu, Debian, Arch, or Gentoo etc.?

2

2 Answers 2

5

The reason you didn't find a solution is that there isn't one. There is no such thing as “the current keyboard layout”, if by this you mean something like “US QWERTY layout”. The keyboard layout doesn't have to be any particular predefined layout. The only way to describe it unambiguously is to dump the whole set of mappings (which you can do with xkbcomp :0 -).

You can find out which layout is configured in this or that configuration interface. All that will tell you is which layout is configured in that particular interface. It won't tell you whether this layout is current. The user might have used a different interface to change the layout, or might have set a completely custom keymap with a tool like xmodmap or xkbcomp.

The output you get from setxkbmap -query is as close as possible. It gets information from the X server, so you don't run into the problem of configuration files that may or may not have been applied. However, there are two ways to tell the X server to change the keyboard layout: by giving a layout name (and options), or by setting individual keys. If setxkbmap -query says us, it means that the last time somebody successfully asked the X server to load a specific layout, the request was to load the us layout. But if there was a subsequent request to change the configuration of individual keys, the layout may now be completely different.

Nobody is doing anything wrong. It's just that there's no concept of “current layout” in the system in general, because the keyboard layout doesn't have to be a predefined layout.

3
  • Thank you for your reply @Gilles, so how you would get the layout in system if setxkbmap -query doesn't work ? By the meaning of doesn't work I mean that it shows the same output even if layout applet shows different layout. Where then it stores current layout ?
    – Artfaith
    Nov 6, 2017 at 0:15
  • @V.7 If a layout applet shows something different, then it's probably showing the layout that it configured last (either that applet, or another applet that's part of the same desktop environment). If you know which applet was used to set the keyboard layout, set that. If you want a tool-agnostic answer, there isn't one. Nov 6, 2017 at 0:19
  • Why there's no one ? You mean that this's impossible ? Okay ... now we have an option to detect current layout by xkbcomp :0 - somehow ... also ... fix me please if I'm wrong. The applet listens to keybinding event and changes layout without changing it in config files, but in DE. So it stores it somewhere.
    – Artfaith
    Nov 6, 2017 at 0:21
1

It seems this depends more on the current window manager than on the distro. On GNOME (probably on all distros using dconf database (GNOME, MATE/GNOME2, XFCE, Unity)) you can get the current layout from first element of the array returned by

gsettings get org.gnome.desktop.input-sources mru-sources

This might be a start for a script for all distros/WMs. Could use switch based on wmctrl -m, which will be valid on EWMH/NetWM compatible X Window Manager (examples include Enlightenment, icewm, kwin, metacity, and sawfish).

Related info

  • To get the graphical chart I use this alias:

    alias current_keyboard_layout_chart=$'gkbd-keyboard-display -l $(localectl status | awk \'/X11 Layout/ {print $3}\')$\'\t\'$(localectl status | awk \'/X11 Variant/ {print $3}\')'
    
  • Generate a PDF of the layout

    xkbprint -color "${DISPLAY}" - | ps2pdf - > current_keyboard_layout.pdf
    
  • Other ways

    # systemd
    localectl status
    # GNOME
    gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval  "imports.ui.status.keyboard.getInputSourceManager().currentSource.id"
    # KDE
    qdbus org.kde.keyboard /Layouts getCurrentLayout
    # x11-xkb-utils (old)
    setxkbmap -query; setxkbmap -print
    

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .