Override a few keycodes with XKB

5,826

Solution 1

Create a file containing your keycode changes, and save it as (for example) ~/.xkb/keycodes/local. (The keycodes directory is hard-coded; the base directory can be something else, and the filename too.) This will contain in your case

xkb_keycodes {
  <PGUP> = 110;
  <PGDN> = 112;
  <DELE> = 115;
  <INS> = 117;
  <HOME> = 118;
  <END> = 119;
};

To load this, run

setxkbmap -print | sed -e '/xkb_keycodes/s/"[[:space:]]/+local&/' | xkbcomp -I${HOME}/.xkb - $DISPLAY

This outputs your current settings, adds +local to the xkb_keycodes include statement, and loads it into the XKB compiler, adding ~/.xkb to the include path. (If you named your file something other than ~/.xkb/keycodes/local, you'll obviously need to change +local and -I${HOME}/.xkb}.) That way all the other settings are preserved.

Solution 2

You can start by setxkbmap -print > somefile.xkb to get the keymap you are using as a base, and then redefine the key names you want in the xkb_keycodes section like this :

xkb_keymap {
 xkb_keycodes {
  include "evdev+aliases(azerty)"
  // Custom <key name> = keycode
  <INS> = 117;
  <HOME> = 118;
  <PGUP> = 110;
  <DELE> = 115;
  <END> = 119;
  <PGDN> = 112;
 };
 xkb_types { include "complete"};
 xkb_compatibility { include "complete"};
 xkb_symbols { include "pc+fr+inet(evdev)"};
 xkb_geometry { include "pc(pc104)"};
};

This generates warnings that some key names are assigned multiple times, but it works because only the last one count (so it's important to put the custom settings after the include). And the file have the advantage of being readable.

Another way is to dump the keymap you use into a file : xkbcomp $DISPLAY somefile.xkb and modify the same lines (it is the same keymap with the includes evaluated). This way there is no warnings for multiple definitions, but it is less readable.

Share:
5,826

Related videos on Youtube

Gilles 'SO- stop being evil'
Author by

Gilles 'SO- stop being evil'

Updated on September 18, 2022

Comments

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 1 year

    I want to shuffle a few keys around with XKB. (Why? On a laptop where some keys are inconveniently located.) I currently use xmodmap:

    keycode 110 = Prior
    keycode 115 = Delete
    keycode 112 = Next
    keycode 117 = Insert
    keycode 119 = End
    keycode 118 = Home
    

    Instead I want to use XKB and assign different symbolic names for certain physical keys, rather than assign different keysyms to certain keycodes. (This is why.) I want keycode 110 to send PGUP instead of HOME, keycode 115 to send DELE instead of END, etc. The rest of the configuration must not be affected (so PGUP is to keep sending the keysym Prior, etc., and all other keys remain as they are).

    How can I change the assignment of these specific keycodes? I'll load a file with xkbcomp somefile.xkb $DISPLAY, what do I need to put in somefile.xkb?

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 9 years
    Where are all these names (evdev+aliases(azerty), etc.) coming from? I explicitly want to perform a relative change, not a full reconfiguration. Keys other than the 6 listed in the questions must not be affected, they must remain in the state they were in when I issued the command.
  • Leiaz
    Leiaz about 9 years
    They come from the setxkbmap -print, they are files from /usr/share/X11/xkb/. I think you have to give a whole keymap to xkbcomp but I could be wrong ...
  • L. Levrel
    L. Levrel about 8 years
    So this is the expected path and way to pass it to the -I switch! Thanks, I was unable to figure this out from the manpage.
  • zw963
    zw963 almost 6 years
    worked like a charm! it worked if want update xkb_symbols, just replace xkb_keycodes with it, worked