Customize Keyboard Mappings with Wayland – How to Guide

keyboard shortcutswayland

I use Linux on a Macbook with a UK/GB keyboard and I customize the keymap to solve some problems Apple's weird keyboard layout causes. I use xmodmap to do this. I'd like to try Wayland, but xmodmap doesn't work in that. How can I achieve a similar result in Wayland? The .Xmodmap file I use contains the following:

keycode  12 = 3 numbersign 3 sterling sterling sterling threesuperior sterling
clear Control
clear Mod4
add Control = Control_L Super_R
add Mod4 = Super_L
keysym Caps_Lock = NoSymbol Caps_Lock

Line 1: On UK keyboards Shift3 is £, so # usually has its own key near Return. But on the Mac # is obtained with Altgr3. As a programmer I use # far more than £ so this line swaps them over. Selecting US layout also achieves this, but doing that in Linux also swaps some other commonly used keys, whereas in OS X those other keys are unaffected by US/UK.

Lines 2-5: Make the right Cmd key act as Right-Ctrl, because this keyboard has no physical right Ctrl key.

Line 6: Makes CapsLock only work if you press it with Shift. Not Mac-specific, this should be a standard option for all OSs and keyboards.

Best Answer

Unfortunately modifying the system XKB database in /usr/share/X11/xkb is the only way; from your other question it looks like you've gotten that part working.

The limitation is mostly due to the immaturity of Wayland and a design oversight in XKB.

  • Tools like setxkbmap and xkbcomp provide an -I option to add a user-defined database to search (eg ~/.xkb or ~/.config/xkb, with files and subdirectories laid out like the system database). These tools interact with the X server, so they might be useful configuring the Xwayland compatibility layer for running X applications under Wayland. But they do not at present speak the Wayland protocols.

  • Wayland protocols are still maturing. Currently it seems the input-method and text-input protocols are most relevant, and both are unstable. Neither mention anything about altering a defined keymap; these details are left to the compositor.

  • GNOME and KDE both provide keyboard-handling settings daemons that should handle system XKB options, including changing on the fly. To the best of my knowledge, there's no way to tell either about user customizations. As far as I know, Weston and other compositors rely on config files or environment variables to set XKB options at startup, and provide no way to change them other than exiting and restarting.

  • Even in XKB itself, this is not fully supported. Your custom symbols file can include other system symbols files. But at present there's no include functionality for XKB rules files, so even if you had a tool that would talk to the Wayland compositor and would look up your personal customizations, you'd have to manually include all the rules you want to use (ie copy rules/evdev* from the system XKB and modify it). libxkbcommon has an open issue on this topic and a related bug.