Remapping power key to delete

keyboard-layoutx11xkbxmodmap

I have a macbook pro running Ubuntu 13.10 with FluxBox. Unfortunately, the power button is physically above the backspace where I would expect the delete key to be in. In OS X, the PowerKey program can be used to remap the power key to delete. How can I do something similar in X Windows?

I disabled the power key by editing /etc/systemd/logind.conf and setting HandlePowerKey = ignore After this, I can press the power button without shutting the laptop down.

Running xev -event keyboard reveals that the KeyPress for the power key is normally being eaten by another process:

MappingNotify event, serial 36, synthetic NO, window 0x0,
    request MappingKeyboard, first_keycode 8, count 248

KeymapNotify event, serial 36, synthetic NO, window 0x0,
    keys:  1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   
           0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   

KeymapNotify event, serial 37, synthetic NO, window 0x0,
    keys:  1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   
           0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   

However by running only xterm directly from console, I was able to successfully get the keycode and remap the key.

xinit /usr/bin/xterm -- :1
xev -event keyboard | tee /tmp/junk.txt
tail -n ???? /tmp/junk.txt
MappingNotify event, serial 28, synthetic NO, window 0x0,
    request MappingKeyboard, first_keycode 8, count 248

KeyPress event, serial 28, synthetic NO, window 0x600001,
    root 0xa1, subw 0x0, time 720369, (146,89), root:(148,91),
    state 0x0, keycode 124 (keysym 0x1008ff2a, XF86PowerOff), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyRelease event, serial 28, synthetic NO, window 0x600001,
    root 0xa1, subw 0x0, time 720369, (146,89), root:(148,91),
    state 0x0, keycode 124 (keysym 0x1008ff2a, XF86PowerOff), same_screen YES,
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False

MappingNotify event, serial 29, synthetic NO, window 0x0,
    request MappingKeyboard, first_keycode 8, count 248

At this point, it was also possible to map the key using xmodmap. I mapped it to backslash which is easier to see than delete:

 xmodmap -e "keycode 124 = backslash bar"

I could get a barely functional equivalent to run by adding this to ~/.fluxbox/keys but this is both ugly and slow enough to be unusable.

  # Remap the power off to a delete key...
  XF86PowerOff :Exec xdotool key Delete

How can I use xmodmap or setxkbmap to use the power button at the same time as a normal window manager?

Best Answer

I don't exactly understand the final sentence, question, but I can relate to the title of the question: "Remapping power key to delete".

Try replacing any "XF86PowerOff" or just the one corresponding to your keyboard with "Delete" in /usr/share/X11/xkb/symbols/inet.

You need to repeat this after a system update which updates that file.

Related Question