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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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.
Method 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0