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

I.e. xset -q | grep LED| awk '{ print $10 }' shows different output with different keyboard layouts for me, 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 way to get the current 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 for each distribution ? I.e. for script which will execute command to get current keyboard layout depending on distribution. I.e. which command will output CURRENT toggled layout for Fedora, which one for Ubuntu, KDE Neon, Arch, Gentoo etc.

  • 1
    To people voting to close because of the Ask Ubuntu question: no, this is a different question. An Ubuntu-specific answer would not work in general. – Gilles Nov 5 '17 at 23:53

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.

  • 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 ? – V.7 Nov 6 '17 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. – Gilles Nov 6 '17 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. – V.7 Nov 6 '17 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.