Disable numlock, preserving mouse button key bindings

Based on @Michał Górny's answer. Here are the commands to disable num-lock, use numbers anyway, and map numlock to F13 (may be handy to bind to a special function in your window manager).

# NumLock is F13
xmodmap -e "remove mod2 = Num_Lock" \
        -e "keycode 77 = F13"

# Use numbers even when numlock is off
xmodmap -e "keysym KP_End = KP_1" \
        -e "keysym KP_Down = KP_2" \
        -e "keysym KP_Next = KP_3" \
        -e "keysym KP_Left = KP_4" \
        -e "keysym KP_Begin = KP_5" \
        -e "keysym KP_Right = KP_6" \
        -e "keysym KP_Home = KP_7" \
        -e "keysym KP_Up = KP_8" \
        -e "keysym KP_Prior = KP_9" \
        -e "keysym KP_Insert = KP_0" \
        -e "keysym KP_Delete = KP_Decimal"

Its nice not to have the numlock light on all the time :)


Let's start with some explanation of what is happening and why your idea doesn't work. First, take a look at the modifier map:

$ xmodmap
xmodmap:  up to 3 keys per modifier, (keycodes in parentheses):

shift       Shift_L (0x32),  Shift_R (0x3e)
lock        Caps_Lock (0x42)
control     Control_L (0x25),  Control_R (0x69)
mod1        Alt_L (0x40),  Meta_L (0xcd)
mod2        Num_Lock (0x4d)
mod3      
mod4        Super_L (0x85),  Super_L (0xce),  Hyper_L (0xcf)
mod5        ISO_Level3_Shift (0x5c),  Mode_switch (0xcb)

As you can see, Num_Lock is mod2 here. When it is on, all keypress events come with mod2 bit on.

If you disable it like this:

$ xmodmap -e "keycode 77 = NoSymbol"
$ xmodmap 
xmodmap:  up to 3 keys per modifier, (keycodes in parentheses):

shift       Shift_L (0x32),  Shift_R (0x3e)
lock        Caps_Lock (0x42)
control     Control_L (0x25),  Control_R (0x69)
mod1        Alt_L (0x40),  Meta_L (0xcd)
mod2        BadKey (0x4d)
mod3      
mod4        Super_L (0x85),  Super_L (0xce),  Hyper_L (0xcf)
mod5        ISO_Level3_Shift (0x5c),  Mode_switch (0xcb)

Note that mod2 is now associated with BadKey, and this seems to confuse Xorg a lot. In fact, most of modifier map changes seem to break X11 for me.


Right now, I can't find a good solution that involves playing with modifier map. But I have another idea: you can map all keypad keys to work the same with num lock both on and off. That is:

xmodmap -e "keysym KP_Up = KP_8"
xmodmap -e "keysym KP_Left = KP_4"
# ...

etc. Once you do that, the state of num lock will no longer matter.

You can even remove the modifier afterwards to turn the LED switching off:

xmodmap -e "remove mod2 = Num_Lock"