Disable numlock, preserving mouse button key bindings

5

2

I'm having trouble to disable the Numlock key (make the key do nothing, so that the numpad numbers always work) while at the same time mapping extra mouse buttons (at the side of my mouse) to shortcuts to perform custom actions.

I use Arch Linux and Xfce and the Login Manager is LightDM.

I use this code, put in a bash file, to disable my numlock on startup:

xmodmap -e "keycode 77 = NoSymbol"

And I have a .xbindkeysrc file in my home directory, with:

"xdotool key ctrl+w"
 b:8 + Release

"xdotool key ctrl+d"
 b:9 + Release

(Close tab & bookmark)

If I enable the "disable Numlock" code at startup, the .xbindkeysrc file has no effect anymore. This seems to be related to the command numlockx on which is in the same bash file as the xmodmap command (before it). How can I solve it?

Adding

"NoCommand"
 c:77

to .xbindkeysrc does not have the desired effect either. Changing c to m because Numlock in theory is a modifier key, doesn't help. Using Num_Lock instead of c:77 also does not work.

Smile4ever

Posted 2014-11-01T08:06:01.667

Reputation: 209

Answers

4

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"

Michał Górny

Posted 2014-11-01T08:06:01.667

Reputation: 253

If I remove the numlock modifier key, at least my alt and altgr keys break. I guess I have to live with the numlock light. Thank you very much, it's working. I'll post what I am using now. – Smile4ever – 2014-11-01T16:11:46.693

5

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 :)

ideasman42

Posted 2014-11-01T08:06:01.667

Reputation: 591

Works beautifully, thank you. How can I automate this at startup? Just adding the script to my startup doesn't work. For what's worth, I'm using systemd. – Smile4ever – 2015-05-25T07:59:55.967

Update: it does work with sleep 7 and numlockx above your script. However, pressing numlock the led light goes off again :( This is only the case when I use it as startup script. – Smile4ever – 2015-05-25T08:10:22.657

Removing the modifier somehow breaks Alt+Tab. Can I put these commands in .Xmodmap? – Smile4ever – 2015-05-25T13:15:11.353

1@Smile4ever, I put this in my .xinitrc but it could probably go in ~/.Xmodmap too. Odd, Alt+Tab still works for me. – ideasman42 – 2015-05-26T03:48:36.607

@Smile4ever. "pressing numlock the led light goes off again", I had this problem too, But "remove mod2 = Num_Lock" fixed this for me. – ideasman42 – 2015-05-26T03:50:09.590

Reassigning the num lock key or removing the key modifier function breaks my shortcut keys. So instead I will only use the lower part of the solution. I will try to remedy the numlock light to be always on with some other solution. :) – Smile4ever – 2015-05-26T07:46:57.940

Okay, I found the solution to reassign my numlock key and not to break my shortcut keys :) I have put xmodmap -e "keycode 77 = F13 Num_Lock F13" after your last line and it seems to work :) – Smile4ever – 2015-05-26T08:16:48.117

0

I think xmodmap should not be used when xkb is enabled (which is always nowadays). Here is an xkb-based solution: change numlock's keycode with an unused key's code:

/usr/bin/numlockx on
/usr/bin/xkbcomp $DISPLAY /tmp/kbmap.xkb
sed -i.bak 's/<NMLK> = 77;/<NMLK> = 98;/
            s/<KATA> = 98;/<KATA> = 77;/' /tmp/kbmap.xkb
/usr/bin/xkbcomp /tmp/kbmap.xkb $DISPLAY

Lorinczy Zsigmond

Posted 2014-11-01T08:06:01.667

Reputation: 101

0

  • Xfce keyboard settings: uncheck restore numlock state (this turns the light off when it's already on)
  • Xfce startup settings: add a mention to the script: sh /home/$USER/numlockalwayson.sh

Script:

#timeout, otherwise it will not work upon boot/login (make two variants of the script. Use sleep 2 for the login variant)
sleep 20
xmodmap -e "keysym KP_Up = KP_8"
xmodmap -e "keysym KP_Left = KP_4"
xmodmap -e "keysym KP_Right = KP_6"
xmodmap -e "keysym KP_Down = KP_2"
#
xmodmap -e "keysym KP_Begin = KP_5"
#
xmodmap -e "keysym KP_Home = KP_7"
xmodmap -e "keysym KP_Next = KP_3"
xmodmap -e "keysym KP_Prior = KP_9"
xmodmap -e "keysym KP_End = KP_1"
#
xmodmap -e "keysym KP_Insert = KP_0"
xmodmap -e "keysym KP_Delete = KP_Decimal"
#
numlockx on

This light gets switched off when you press Num_Lock. That's the only shortcoming of this solution, and is a huge improvement of what I had. This solution does not break other modifier keys like Alt.

Great thanks to Michał Górny for this solution.

Bonus

If you log off and log in again, the script no longer works (you have to reboot to make it work again). To fix that, edit /etc/lightdm/lightdm.conf (the location where it's located in Manjaro Linux) and add the login variant of the script:

session-setup-script=sh /home/$USER/numlockalwayson-login.sh

Also, LightDM switches the num lock off if you log off. To correct that, add to the same file:

greeter-setup-script=/usr/bin/numlockx on

Don't forget to save.

You need both Xfce startup and this configuration to make it work always.

Smile4ever

Posted 2014-11-01T08:06:01.667

Reputation: 209