17

Due to a coffee accident some numbers of my laptop's keyboard stopped working. I used xmodmap to use F1, F2, etc, as replacements and saved the configs to ~/.Xmodmap.

However, that caused a few problems.

  1. I don't always use X on this computer, and without starting X xmodmap does not apply.

  2. It causes X to take more time to start.

  3. For some reason it caused XFCE keyboard shortcuts that had nothing to do with any of the keys changed via xmodmap to stop working (in fact, all keyboard shortcuts stopped working, except for the window manager shortcuts). After a few minutes passed since I start X, the XFCE shortcuts start working normally again! This lag isn't very annoying, but is also an issue.

I imagine there is some kind of mapping that is read by the OS before X starts. Isn't there a way to change that mapping? Is it any way to change the keyboard mapping w/o using X tools? Im using Debian stable.

PS: Apparently the file that calls xmodmap .Xmodmap on startx is /etc/xdg/xfce4/xinitrc. It's contents can be found here.

Alex
  • 2,106

3 Answers3

25

First, the good news

The Linux system console absolutely has its own set of keyboard mappings, which can be managed using the tools from the kbd package, specifically dumpkeys and showkey for discovery and loadkeys to load in a customized mapping. The SuperUser question How to change console keymap in Linux? has an answer with good information on how to use these tools.

Now, the bad news

While it's true that those tools will allow you to remap the keys on the Linux console, without involving xmodmap or requiring that X be running, they will only affect the keymappings on the virtual text console. The changes will have absolutely no effect in the graphical environment, because X's XInput/evdev system reads from the input devices directly and does its own processing.

So, if you were hoping to avoid using xmodmap by just remapping on the console and having it apply everywhere, I'm afraid that won't work. In fact, you'd need to remap both the console (using loadkeys) and X11 (using a method like xmodmap), to use the same keyboard layout everywhere.

The solution to the xmodmap slowness (and bugginess, since its remappings are glitchy and non-persistent in desktop environments that use layout switching) would be to define an entirely new keyboard layout based off of whatever layout you were previously using, rather than applying runtime modifications. On X startup, you'd load that new, remapped layout instead of whatever you're using now. (It seems this is now the only way to reliably modify the keyboard layout in recent Ubuntus — and possibly other distros — as their xmodmap is no longer useful.)

For information on defining and using a custom xkb keyboard layout, see:

  1. Howto: Custom keyboard layout definitions in the Ubuntu Community Wiki.
  2. How to modify a keyboard layout in Linux, a blog post by Romano Giannetti.

Both were written this year (2014), so the information should be current. The Ubuntu wiki information should be applicable to any distro, for the most part, as they all use the xkb system in X.

FeRD
  • 1,394
8

There are actually ways to remap at a level low enough to apply to the whole system, X11 or not, and they've become more important than ever now that we're seeing uptake of Wayland compositors which neglect to expose a UI for libinput's remapping support.

You basically need to reconfigure how the kernel's input layer translates raw scancodes into keycodes before they reach the console or the evdev API that X11 and Wayland sit on top of.

I'm aware of two ways to do that:

  1. Modify the hardware database (hwdb) entry for your keyboard. udev lets you do that by adding rules files to /etc/udev/hwdb.d/ and triggering a database rebuild with systemd-hwdb update, and then forcing it to be applied without a restart via udevadm trigger.

    This ArchiWiki page has full instructions and explicitly says that it'll work for both X11 and console input.

  2. There's a daemon named evdevremapkeys which was specifically written for remapping key events on evdev devices to monkey-patch remapping support into evdev clients which don't support them, like Wayland compositors.

    It basically uses the same approach as userspace drivers like G15Daemon which need to compensate for non-standard input devices. (Open up the evdev device, swallow any events it intends to remap, so nothing else listening on the device can see them, then emit the corrected events via the uinput API for creating kernel-level input devices from userspace.)

ssokolow
  • 1,127
0

The X-specific tools did not work for me despite using the X display server. I did not want to code it by myself. I searched for one day until I found a working solution. And better, it also might work for other distributions.

I found and tested KMonad as a first time user, and it's baffling nobody mentioned it before. It is available for only very specific package managers but you can automatically build it with Docker which worked for me but took quite some time.

KMonad reads a text configuration file as instructions how to modify the keyboard which is independent of the display server. It uses ioctl and works as long as the process is running. You only need to run it on startup with super user (sudo) rights. (Edit: might work with systemd.) Good thing: when started, it survives sleep mode or any keyboard related switching and deactivation because KMonad continues to run in the background.

KMonad also supports more powerful features like macros, multiple keyboard contexts (which it calls "layer") and shell command execution. I now hide this because it wasn't asked for. The power originates from the composability of individual features in the configuration.

They write, it runs on Windows and Mac (which I did not try).

There is a VSCode syntax highlighting extension for the configuration file if you mind. My only perceived imperfection is the lack of a GUI. But I think, it would be well suited for practicing GUI development if someone wants to try.

(Slightly edited after spam accusation. I am sorry, if the answer is misfit to the question.)