Happy Hack Tip: remap your keyboard buttons

Occasionally you get a keyboard which sports some buttons in odd places, in my case, the Ctrl and Ins buttons were swapped. Strange layout, no doubt. This frustrates anyone who writes code, or uses mouseless surfing, as you use key-combinations and the Alt / Ctrl keys plenty.

Luckily we can use xmodmap to remap your keys to any other we prefer, here is how…

you will need: keyboard, flat head screwdriver, emperor penguin plushy

you will need: keyboard, flat head screwdriver, emperor penguin plushy

I want to swap the Ctrl and Ins buttons. In a terminal we run xev. It presents a window that captures keystrokes, and prints the keycode for key presses:

keycode 105 (keysym 0xff63, Control_R)
keycode 118 (keysym 0xffe4, Insert)

We want to swap those two around, so that keycode 105 becomes Insert, and 118 becomes Control_R.

I popped the buttons out with a screwdriver, and swapped them around. Next we write a xmodmap config file to swap the keycodes: pico ~/scripts/xmodmap-config

! comments start with a bang
! remap our keycodes to new functions
keycode 118 = Control_R
keycode 105 = Insert

! clear and reset the control modifiers
! refresh the modifier list
! with both left and right Ctrl buttons
clear control
add control = Control_L Control_R

taking back control

taking back control

We test this by running xmodmap ~/scripts/xmodmap-config. The keys should now be swapped until you log out.

If we place this same command under System Preferences > Startup Applications, it won’t work, I found there needs to be a small delay before we run xmodmap. I’m unsure why, to be honest, but we can work around this issue with a small shell script, that will sleep for a second, then run xmodmap for us. We create ~/scripts/remap-keyboard.sh:

#!/bin/bash
sleep 1 && xmodmap ~/scripts/xmodmap-config

let’s make the script executable:

chmod u+x ~/scripts/remap-keyboard.sh

and we add this shell script to Startup Applications, under System Preferences, using this as the command:

sh ~/scripts/remap-keyboard.sh

This should keep me happy, until I can get my hands on a Happy Hacker Keyboard. :-)

Both comments and pings are currently closed.

One Response to “Happy Hack Tip: remap your keyboard buttons”