5

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.

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"
|improve this answer|||||
  • 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 Nov 1 '14 at 16:11
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 :)

|improve this answer|||||
  • 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 May 25 '15 at 7:59
  • 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 May 25 '15 at 8:10
  • Removing the modifier somehow breaks Alt+Tab. Can I put these commands in .Xmodmap? – Smile4ever May 25 '15 at 13:15
  • 1
    @Smile4ever, I put this in my .xinitrc but it could probably go in ~/.Xmodmap too. Odd, Alt+Tab still works for me. – ideasman42 May 26 '15 at 3:48
  • @Smile4ever. "pressing numlock the led light goes off again", I had this problem too, But "remove mod2 = Num_Lock" fixed this for me. – ideasman42 May 26 '15 at 3:50
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.

|improve this answer|||||
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
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.