A Linux kernel module for remapping keyboard scan codes.
kmap allows you to customize your keyboard layout by remapping scan codes directly in the Linux kernel. This works at a low level, providing an effective solution for:
- Swapping keys (e.g., CapsLock and Ctrl)
- Fixing broken keyboard keys
- Creating custom keyboard layouts
- Linux kernel headers
- Build tools (make, gcc)
make
sudo insmod kmap.koTo load with default mappings:
sudo insmod kmap.ko default_map="capslock:leftctrl,leftctrl:capslock,zenhan:esc"List current mappings:
cat /sys/kernel/debug/kmap/controlSet mappings (format is scancode_src:scancode_dest,...):
echo "capslock:leftctrl,leftctrl:capslock" > /sys/kernel/debug/kmap/controlReset keys to default:
echo "capslock:capslock,leftctrl:leftctrl" > /sys/kernel/debug/kmap/controlFor available keynames, please look at key_table in kmap.c.
kmap uses a kprobe to intercept keyboard scan codes at the atkbd_receive_byte function. The module maintains a mapping table and provides a debugfs interface at /sys/kernel/debug/kmap/control.
You can customize the hook point by modifying the SYMBOL_NAME define in the source code. This allows you to adapt kmap to different keyboard drivers or input subsystems, such as serio_interrupt() for broader device support, as long as the position of the argument is preserved.