Custom XKB symbols

My laptop has a Japanese keyboard, but I still need quick access to some symbols like £€®©

My easy solution to this is to set the special char key to R-ctrl
& then to edit /usr/share/X11/xkb/symbols/jp

I can just add a few extra 3rd level symbols like this
key <AE04> { [ 4, dollar, EuroSign ] };

EASY!

The only problem is that this file gets overwritten after an xkb update.
Its only a 2 min job to fix it, but is there a more permanent method that isn’t a difficult task?

I was hoping maybe I can duplicate the /symbols/jp as /symbols/jp_en & have it show as an alternate layout in gnome>settings>keyboard
I’m not sure if that’s possible or what else I might need to edit to have my duplicate file become a system option…

I have studied the Arch xorg keyboard pages, on & off for about a year, but I’ve reached my limit with my small brain in comprehending.

Not sure about the GUI side.

You can make a variant with just the changes, Copy the file as you suggest then you can do setxkbmap jp_en. However it needs to be run every time you boot/login (a startup app/script etc) or re-plug the keyboard (udev).

Here’s a basic example for a UK keyboard, hopefully it will help:

# /usr/share/X11/xkb/symbols/myxkb 
# to apply: setxkbmap myxkb  
default partial alphanumeric_keys
xkb_symbols "basic" {

    include "latin"
    name[Group1]="English myxkb";

    key <AD10>  { [     p,    P,          apostrophe,          bar ] };

    include "level3(ralt_switch)"
}
I'd either do that or use `xkbcomp`.

In that post I forgot to mention xmodmap is by far the slowest, it takes several seconds to apply the config, the others are much quicker.

EDIT: I was mostly going from memory, which is not as good as it used to be :frowning:, the file to be used with that command was actually an edited copy of the original. I can’t remember if I ever knew how to use the partial file properly since I can’t get it working atm. I haven’t got time to play around with it today.

Also gnome uses wayland by default, and this will only work for X and maybe xwayland. See below.

Thanks @dmt

It seems like quite an easy step:
I duplicated my symbols/jp file as a personal one symbols/jp_rico
& added setxkbmap jp_rico to ~/.xinitrc since this file already exists.

However, when I run setxkbmap for the current session I get a warning…

setxkbmap jp_rico
WARNING: Running setxkbmap against an XWayland server

The new level3 key I added to jp_rico to test with is not functional. However setxkbmap does say that layout is in use.

setxkbmap -print -verbose 10
Setting verbose level to 10
WARNING: Running setxkbmap against an XWayland server
locale is C
Trying to load rules file ./rules/evdev...
Trying to load rules file /usr/share/X11/xkb/rules/evdev...
Success.
Applied rules from evdev:
rules:      evdev
model:      pc105
layout:     jp_rico
options:    lv3:switch
Trying to build keymap using the following components:
keycodes:   evdev+aliases(qwerty)
types:      complete
compat:     complete
symbols:    pc+jp_rico+inet(evdev)+level3(switch)
geometry:   pc(pc105)
xkb_keymap {
	xkb_keycodes  { include "evdev+aliases(qwerty)"	};
	xkb_types     { include "complete"	};
	xkb_compat    { include "complete"	};
	xkb_symbols   { include "pc+jp_rico+inet(evdev)+level3(switch)"	};
	xkb_geometry  { include "pc(pc105)"	};
};

It seems that an alternative method is to use localectl but I’m a bit unsure about using that correctly & some guidance would be good.
It seems like the only change to 00-keyboard.conf needed is Option "XkbLayout" "jp_rico" & that needs to be applied by localectl as you shouldn’t edit the file manually.

Does this look right?

localectl --no-convert set-x11-keymap jp_rico

Sorry, I saw the xkb config and thought X, but you’re using wayland which is default in gnome :man_facepalming:. Apparently it should work for programs that run through xwayland, but wayland/gnome devs seem to have forgotten that people need to edit keymaps, I don’t use either of them so there may be things I’m missing.

I haven’t used localectl much but it seems right, it works on my end, 00-keyboard.conf has the new value, unfortunately it doesn’t have any effect even after a reboot. I’m on mate though so… :man_shrugging:

I think support for localectl will depend on gnome. Have you tried it?

Have you tried xkbcomp? I don’t think it’ll work but :man_shrugging:

From a quick search there might be other ways. If not then keep a copy of your edited file (the full version) and make a script to copy it over (or just edit the file in place), at least then it’s a 2 second job and you could maybe make a hook to do it automatically after an update.

https://unix.stackexchange.com/questions/292868/how-to-customise-keyboard-mappings-with-wayland
https://unix.stackexchange.com/questions/294286/how-can-i-autoload-xkb-customisations-in-gnome
https://unix.stackexchange.com/questions/212573/how-can-i-make-backspace-act-as-escape-using-setxkbmap

Thanks for your input, its all helpful.

Same experience here after trying. :thinking:

Excellent idea, will do this.

Not yet, but ill give it a try. Thanks again for your help exposing all these options.

EDIT: Interestingly I explored further with Archcraft under X11. But it still doesn’t work.
I created my /usr/share/X11/xkb/symbols/jp_rico duplicate file & issued setxkbmap jp_rico
There was no warning this time & it seems to be set in temp rules just as Gnome wayland.
However, there is still no new keys functioning.
I made sure to do a very simple & confirmed working edit, just adding ® to Q. R® works fine as per my jp file, but Q doesn’t which is part of the jp_rico
So even with X11 this isn’t working, for the current session at least.
Archcraft doesn’t have a default .xinitrc or .profile so I didn’t take it further.

Today I will look at xkbcomp

1 Like

Why not just automate the modification of this file if it’s so easy :slight_smile:

Use this pacman hook (you can customize the Exec line to suit your needs). It will run every time xkeyboard-config is updated.

sudo mkdir -p /etc/pacman.d/hooks
sudo touch /etc/pacman.d/hooks/xkeyboard-config.hook
[Trigger]
Operation = Install
Operation = Upgrade
Type = Package
Target = xkeyboard-config

[Action]
Description = Add a few extra 3rd level symbols...
Exec = /bin/sh -c "sed -i 's/4, dollar/4, dollar, EuroSign/' /usr/share/X11/xkb/symbols/jp"
When = PostTransaction
1 Like

Making a copy and editing it works for me, are you sure your edits are correct? It should work straight away, like below (not simulated btw).

pPþÞ 
setxkbmap gb_test
pP'|
gb_test
// Keyboard layouts for Great Britain.

default partial alphanumeric_keys
xkb_symbols "basic" {

    // The basic UK layout, also known as the IBM 166 layout,
    // but with the useless brokenbar pushed two levels up.

    include "latin"

    name[Group1]="English (UK)";

    key <TLDE>	{ [     grave,    notsign,          bar,          bar ]	};
    key <AE02>	{ [         2,   quotedbl,  twosuperior,    oneeighth ]	};
    key <AE03>	{ [         3,   sterling, threesuperior,    sterling ]	};
    key <AE04>	{ [         4,     dollar,     EuroSign,   onequarter ]	};

    key <AC11>	{ [apostrophe,         at, dead_circumflex, dead_caron]	};
    key <BKSL>	{ [numbersign, asciitilde,   dead_grave,   dead_breve ]	};

    key <LSGT>	{ [ backslash,        bar,          bar,    brokenbar ]	};

    # I only added this line
    key <AD10> { [p, P, apostrophe, bar ] };

    include "level3(ralt_switch)"
};

...
# the rest of the file is unchanged but quite long

@stasadev

Definitely the easiest and most sure option. :slight_smile:

Yes, I’m sure. I thought this might be a query ~
here is a snippet of the singular change

    key <AD01> { [ q, Q, registered			] };
    key <AD02> { [ w, W			] };
    key <AD03> { [ e, E			] };
    key <AD04> { [ r, R, registered			] };

Q I added registered, R is existing & working. setxkbmap jp_rico but it does not have effect.

   ~ echo $XDG_SESSION_TYPE                                             
x11
   ~ setxkbmap -print -verbose 10                   
Setting verbose level to 10
locale is C
Trying to load rules file ./rules/evdev...
Trying to load rules file /usr/share/X11/xkb/rules/evdev...
Success.
Applied rules from evdev:
rules:      evdev
model:      pc105
layout:     jp_rico
options:    lv3:switch
Trying to build keymap using the following components:
keycodes:   evdev+aliases(qwerty)
types:      complete
compat:     complete
symbols:    pc+jp_rico+inet(evdev)+level3(switch)
geometry:   pc(pc105)
xkb_keymap {
	xkb_keycodes  { include "evdev+aliases(qwerty)"	};
	xkb_types     { include "complete"	};
	xkb_compat    { include "complete"	};
	xkb_symbols   { include "pc+jp_rico+inet(evdev)+level3(switch)"	};
	xkb_geometry  { include "pc(pc105)"	};
};
   ~ ££££®®®®qqqq

Although I am doing this above test on Archcraft X11, so perhaps there is some difference to Manjaro.
If I login as Manjaro Gnome xorg, would this /should this/ work?

Looks like it should work. :man_shrugging:

Just checking, there’s only one definition for each key?

It doesn’t seem to apply here, but if you remove a definition it will remain until a reboot (or maybe logout/login). Additions or replacements should work straight away.

I’ve had trouble switching a media key with a normal key, yet other people haven’t. It’s a complex system built to handle lots of different configurations, there may be bugs, or results may vary depending on hardware, software, distro, DE etc. :man_shrugging:

I haven’t used Gnome since version 2.x but AFAIK it should might work. xkb is the X keyboard extension, it handles the keyboard configuration for xorg, however it may not be the only thing that matters. KDE and Gnome in particular like to do things differently.

The whole thing is pretty complex and doesn’t seem to be well documented. If you haven’t already, I’d suggest you go with the hook, it should work for both wayland (according to your OP) and xorg and is definitely much quicker and easier.

You can always continue down the xkb rabbit hole if you wish, I do on occasion. I’ll help however I can, but at this point I think you probably know as much as I do.