2

The Numlock key sees little action in today's day and age, yet it's location in the (standard) keyboard layout is quite prominent.

Since I use a lot of spreadsheets, using the keypad for data entry is very convenient, with one caveat: the equal sign is quite hard to reach, when your right hand is on the keypad.

My idea was to re-map Numlock, such that it can act as both, Numlock and equals, with the former accessible through a modifier.

For this purpose I decided to make my own xkb keymap, as I tried the same thing with xmodmap but I had similarly bad results.

(file name is pb)

default partial alphanumeric_keys
xkb_symbols "basic" {
    
    name[Group1] = "PB (US intl with level 3 and level 5";
    key.type[Group1] = "EIGHT_LEVEL";

    include "us(altgr-intl)"

    //              (norm)     NLOCK     AltGr
    key.type[Group1] = "FOUR_LEVEL_MIXED_KEYPAD";
    key <NMLK> { [ KP_Equal, KP_Equal,  Num_Lock, Q ] };

    include "level3(ralt_switch)"
    include "level5(rctrl_switch)"
};

Now, as you can see, I added level 3 and level 5, when I want to expand my custom layout later, but I tested withou it, and it doesn't interfere with Numlock.

The behavior resulting from this is as follows when pressing the Numlock key:

(numlock is off)

  • Numlock: '='
  • Shift + Numlock: '='
  • AltGr + Numlock: (switches Num lock on)
  • AltGr + Shift + Numlock: 'Q' (I added this for testing)

This is alright, printing 'Q' when holding down AltGr and Shift was just to see if I can reach that shift level. However when Num lock is on, the behavior not satisfactory at all:

(numlock is on)

  • Numlock: (switches Num lock off)
  • Shift + Numlock: (switches Num lock off)
  • AltGr + Numlock: (switches Num lock off)
  • AltGr + Shift + Numlock: (switches Num lock off)

It seems to be impossible to make Num lock behave like a KP_Equal key, no matter what I try.

Originally, I wanted pressing Numlock to print an equal sign, and when holding Shift, to operate Num Lock on and off. Since that didn't seem to be possible, I opted for ISO level 3 (AltGr), but that doesn't seem to work either. What I want is that Numlock prints an equal sign no matter if Num lock is on or off, and switch Num Lock when holding down a mod key (which one, isn't really important to me, preferably Shift or AltGr).

polemon
  • 11,133
  • 11
  • 69
  • 111
  • I think (but I'm not sure) that since `` is affected by the state of the `NumLock` modified, but you don't want it to be, it means it's of the wrong type. Try to have this in the symbols section: `key { type = "TWOLEVEL"; symbols[Group1] = [KP_Equal, Num_Lock]; };` – Gilles 'SO- stop being evil' Aug 04 '20 at 19:40
  • Having that in the file, I'm getting an error: "Error loading new keyboard description". – polemon Aug 04 '20 at 20:59
  • @Gilles'SO-stopbeingevil' OK, it seems the file wants `,` between the elements `key {}`, and the map is accepted, but I'm still getting the same behavior. – polemon Aug 04 '20 at 21:08
  • Yes, my bad, I copied the parts separately and added the wrong punctuation. I'm sure what you want is possible because I have something similar with CapsLock, but I can't figure out how I'm doing that… What does the part of the output of `xkbcomp :0 -` corresponding to `key ` look like? Does `key { [ KP_Equal, KP_Equal, Num_Lock, Q, KP_Equal, KP_Equal, Num_Lock, Q ] };` instead of the 4-element list work any better? – Gilles 'SO- stop being evil' Aug 04 '20 at 21:28
  • @Gilles'SO-stopbeingevil' OK, I did that, now this the behavior: when I have num lock on, and just push Numlock, it prints the equal sign, but also switches num lock off. I'm quite flabbergasted, this isn't what I was expecting at all. – polemon Aug 04 '20 at 21:46
  • btw. same thing when having num lock on, and pressing AltGr + Shift + Numlock, it prints 'Q', and also switches num lock off. – polemon Aug 04 '20 at 21:48
  • And what's the output of `xkbcomp :0 - | sed -n '/key /,/}/p'`? For me, with `CAPS`, I get ` key { type= "PC_ALT_LEVEL2", symbols[Group1]= [ Mode_switch, Caps_Lock ] }; ` and the behavior is that `CAPS` acts as `Caps_Lock` with Alt and as AltGr (`Mode_switch`) without Alt. – Gilles 'SO- stop being evil' Aug 04 '20 at 22:14
  • @Gilles'SO-stopbeingevil' ah forgot that, I'm getting an error: "xkbcomp :0 Invalid MIT-MAGIC-COOKIE-1 keyError: Cannot open display ":0" Exiting " – polemon Aug 04 '20 at 22:21
  • `DISPLAY` is set to `:1.0`, when I do `xkbcomp :1.0` I'm getting the same error. – polemon Aug 04 '20 at 22:24
  • `xkbcomp $DISPLAY - | …` with the dash as the second argument to `xkbcomp` – Gilles 'SO- stop being evil' Aug 04 '20 at 22:45
  • @Gilles'SO-stopbeingevil' sry, about `xkbcomp` incompetence, here's the output: key { type= "EIGHT_LEVEL", symbols[Group1]= [ KP_Equal, KP_Equal, Num_Lock, Q, KP_Equal, KP_Equal, Num_Lock, Q ] }; – polemon Aug 04 '20 at 22:56
  • So you want to arrange for `type` to be something else. `type` determines what modifiers the elements of the `symbols[Group1]` list correspond to. But even if you can't figure out how to set `type`, you should be able to get something acceptable by rearranging the 8 list elements now. – Gilles 'SO- stop being evil' Aug 05 '20 at 06:54
  • @Gilles'SO-stopbeingevil' ok, so the fact that it does two things (switch off numlock and print the character) is normal? Can you suggest what I should use as `type`, perhaps? – polemon Aug 05 '20 at 07:26
  • No, I don't understand that part. It may be due to some other setting but I have no idea what. – Gilles 'SO- stop being evil' Aug 05 '20 at 07:34
  • OK, I've tried all the types I could find in `/usr/share/X11/xkb/symbols/*` none of them change the behavior. When numlock is on, and I press the key, `xev` reports **only** the KP_Equal event, but numlock still goes off. No idea where to go from here. – polemon Aug 05 '20 at 07:51
  • Maybe determine which modifier numlock is with `xmodmap -pm` and clear it with `xmodmap -e 'clear Mod6'` (replace 6 by the correct number). On my system `xmodmap -pm` doesn't report a modifier for CapsLock or NumLock, but they do work the way I've set them up with xkb. I don't understand the interactions between xmodmap and xkb. – Gilles 'SO- stop being evil' Aug 05 '20 at 09:24
  • @Gilles'SO-stopbeingevil' `xmodmap -pm` reports `mod2 KP_Equal (0x4d, BadKey (0xcf)`. I cleared mod2, which got rid of Numlock (the key only worked as KP_Equal). I then re-applied my keymap, and it was back to the same old, with `xmodmap -pm` back to reporting `mod2 KP_Equal (0x4d), BadKey (0xcf)` – polemon Aug 05 '20 at 17:56
  • I don't think I've ever seen this `BadKey`. At a guess it's a symptom of something you need to fix in the xkb code, but I have no idea what. – Gilles 'SO- stop being evil' Aug 05 '20 at 18:14

1 Answers1

0

So. If you look at the ouput of `xev', take attention to the state,

  • If yo keep the keysym Num_Lock on this key, you will see a state of 10 even when pressing the key for the keysym KP_equal , without being locked. And because of it, when you are locked, pressing KP_Equal will unlock you.
  • Now you want to get rid of Num_Lock on this key so it will not generate this pesky state, while still locking on the appropriate layer, it is done like this :
replace key <NMLK> {
type[Group1]= "FOUR_LEVEL_MIXED_KEYPAD",
symbols[Group1]= [ KP_Equal, KP_Equal,  Num_Lock, Q ],
actions[Group1]= [ NoAction(), NoAction(), 
                   LockMods(modifiers=NumLock), NoAction()
};

Take care of the absence of underscore, otherwise it will not work.

  • Now you can verify the state; it should be ok in any case, but oviously this state doesn't mean anything anymore since you have deleted the Num_Lock keysym from your map. So you need to find a spare keycode you do not use, and assign him the Num_Lock keysym. In this example, I'll take the keycode 249
replace key <I249> { symbols[Group1]= [ Num_Lock ] };

Now it should correctly work :)

pillule
  • 65
  • 5
  • I tried your solution, but I still have the effect, that when Num Lock is on, and I press Num Lock, it prints an equal sign, but also switches off Num Lock. At this point I'm starting to believe it might be a problem with the keyboard controller, or something. – polemon Sep 06 '20 at 15:30
  • my bad, I have updated the answer. – pillule Sep 09 '20 at 23:03