XKB group switch

Hi all!

Trying to implement the following for Gnome3 (Wayland) in Fedora 40.

I would like to have a custom xkb symbols file, where I would like to define two layouts.

  1. EURKey Layout
  2. Russian Layout

This because, I would like to override keys globally for both layouts at the same time. For example, change Control and Alt keys.

I have defined the following xkb file test4 and evdev.xml files that live in ~/.config/xkb/symbols and ~/.config/xkb/rules respectively. I am doing this, to escape the need to have scripts or other programs solving this problem. As I would like these files to be part of my dotfiles.

test4 contents:

default partial alphanumeric_keys modifier_keys
xkb_symbols "base" {
    include "eu(basic)"
    include "level3(ralt_switch)"
    name[Group1]="EurKEY";
    
    include "ru(winkeys)"
    name[Group2]="Russian";

    key <CAPS> { 
        [ ISO_Next_Group ]
    };       
};

evdev.xml contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xkbConfigRegistry SYSTEM "xkb.dtd">
<xkbConfigRegistry>
  <layoutList>
    <layout>
      <configItem>
        <name>test4</name>
        <shortDescription>eu2</shortDescription>
        <description>Custom EurKEY and Russian Layout</description>
        <languageList>
          <iso639Id>eng</iso639Id>
          <iso639Id>ger</iso639Id>
          <iso639Id>rus</iso639Id>
        </languageList>
      </configItem>
    </layout>
  </layoutList>
</xkbConfigRegistry>

Then I proceed to set the input-source:

gsettings set org.gnome.desktop.input-sources sources "[('xkb','test4')]"

Everything seems to load, however I notice the following issues:

  • after settings the sources using gsettings, the Russian layout is always selected by default instead of the EURKey layout.
  • the level3 (AltGr+key) symbols from EURKey Layout (e.g. ñ) are only available when Russian group is set. If I press again, I will not be able to access these keys. Not sure if this could be related to the fact that EURKey has defined 4 levels, while Russian layout only has 2.

I believe the solution is quite simple, I just do not see it. Any help to solve this is much appreciated.

Welcome to Fedora @madlinuxer

But to find out the correct syntax would might be helpful to use the tools gnome offers.

Could you config as needed in gnome (maybe using gnome-tweaks)? If yes use the command to extract the entries you need:
gsettings list-recursively org.gnome.desktop.input-sources to see the correct syntax and probably the position it needs to be.

Does this help to debug your script/config file?

Thanks for your reply @ilikelinux.

However, I believe your recommendation will not work. Although gnome-tweaks provide some degree of customization, it does not provide the ability to define my own behavior for the key. Which I want to do in this case.

For my use case I was following the steps described in Custom keyboard layout in GNOME (Wayland) - #7 by amko89

Now, I have indentified a way to have my setup working. Which is bizarre but it seems to work.

If instead of

gsettings set org.gnome.desktop.input-sources sources "[('xkb','test4')]"

I do the following:

gsettings set org.gnome.desktop.input-sources sources "[('xkb','test4'),('xkb','eu')]"

<CAPS> will switch to ru layout.
<SHIFT>+<CAPS> will switch to eu layout. But in this case all level3 symbols are available.

Still I think is very crazy behavior and I still believe there is a better way to do this.

Any help, as always, much appreciated.

It is in the Tweaks > Keyboard section > Additional Layout Option

I change this way the key behavior from my numeric keypad , Del .Del

Is it in your case that you like to have the euroKey right?
There is the currency signs section where you could have a look.

So you could at least find out the correct spelling of the change you try to achieve.

I believe you are missing the whole point. Gnome Tweaks only provides predefined options. I cannot customize them.
This is why I wanted to implement the customization using xkb and evdev.xml files.
Please, read my post once again.

1 Like

Try:

 default partial alphanumeric_keys modifier_keys
 xkb_symbols "base" {
     include "eu(basic)"
     include "level3(ralt_switch)"
     name[Group1]="EurKEY";
    
-    include "ru(winkeys)"
+    include "ru(winkeys):2"
     name[Group2]="Russian";

     key <CAPS> { 
         [ ISO_Next_Group ]
     };       
 };

:warning: But note that it will not work in libxkbcommon 1.8+.

You should use the interface to configure 2 layouts + your option, not altogether. But even so, you should already have everything you need in the advanced keyboard options by default, as @ilikelinux mentioned above. It’s just a matter selecting the relevant options. I see nothing in your setup that requires a custom config.

But if you must, please check the reference doc.

1 Like

@wismill thanks for the information and hint on that starting libxkbcommong 1.8+ the fix will not work.
Well, updated the Fedora 42 and that is the case.

Now, I think I need to clarify my initial intention. I think I missed to do it so far.
The main reason why I want to have everything defined in one place, is because I want to assign specific keys the change of layout.

For example:

  • I have three layouts: eng, ita, rus
  • I want to designate specific keys for each layout. For instance:
    • eng layout will have a designated key: CAPS
    • ita layout will have a designated key: SHIFT+CAPS (or any other key / combination)
    • rus layout will have a designated key: Left ALT+CAPS (or any other key / combination)
  • I want to have the same assignment across all layouts. So, for example: if I am using ita and want to switch to eng I just need to press CAPS, same is true if I use rus layout, pressing CAPS will switch to eng.

So, for the above example, I currently see no way to have this configured in Gnome Tweaks. Of course, I may a newbie, so perhaps I am missing something.

The easiest way would be to configure it without modifying your XKB setup. This is possible in KDE in global shortcuts, but I do not know for Gnome. Better ask in a Gnome forum!

If it is not possible in Gnome, then yes, you’ll have to do it the hard way:

  • Check this doc
  • Look at the current available options for switching
  • Adapt them by gathering the relevant options into one and make it a user option.
  • In order to switch to a specific layout, you’ll need explicit group actions. E.g.:
    xkb_symbols "caps_switch" {
    -  key <CAPS> {[  Mode_switch,  Caps_Lock  ], type[group1] = "PC_ALT_LEVEL2" };
    +  key <CAPS> {symbols=[  Mode_switch,  Caps_Lock  ], actions=[LockGroup(group=1), LockMods(mods=Lock)], repeat=false, type[group1] = "PC_ALT_LEVEL2" };
    };
    
  • But since you want various actions on the <CAPS> key, I’m afraid the existing key types are not enough and you’ll need to design a custom key type too.

This is quite advanced stuff. It’s not extraordinary complicated, but it requires that you take time to read the documentation of the XKB format carefully.

I insist you should read carefully the debugging doc as well, because an error could result in an unusable keyboard. This doc enables to test a XKB config safely.

Happy hacking!