You are not logged in.

#1 2021-07-20 18:35:59

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 67

Set NumLock always on in Xorg

I want numpad keys to behave as NumLock is always on. I know about "numpad:mac" option and have

        Option "XkbOptions" "grp:caps_toggle,grp_led:num,compose:sclk,numpad:mac"

in my /etc/X11/xorg.conf.d/00-keyboard.conf. It works perfectly for most of xorg and console applications in terminal emulators except some (ncurses-based) applications, e.g. minicom, hexedit etc. With "numpad:mac" X still reports numlock state in flags:

NumLock is on (desired behavior):

KeyPress event, serial 28, synthetic NO, window 0x6e00001,
    root 0x2a4, subw 0x0, time 402462037, (79,-13), root:(373,260),
    state 0x10, keycode 87 (keysym 0xffb1, KP_1), same_screen YES,
    XLookupString gives 1 bytes: (31) "1"
    XmbLookupString gives 1 bytes: (31) "1"
    XFilterEvent returns: False

NumLock is off (undesired behaviour):

KeyPress event, serial 28, synthetic NO, window 0x6e00001,
    root 0x2a4, subw 0x0, time 402487204, (819,175), root:(1113,448),
    state 0x0, keycode 87 (keysym 0xffb1, KP_1), same_screen YES,
    XLookupString gives 1 bytes: (31) "1"
    XmbLookupString gives 1 bytes: (31) "1"
    XFilterEvent returns: False

Accordingly ncurses also reports different codes, e.g.:

…
    initscr();
    noecho();
    keypad(stdscr, true);
    for (;;) {
        int c = getch();
        printw("%04X (%i)\n", c, c);
…

reports "0031 (49)" for "1" numpad key when NumLock in "on" and "015F (351)" otherwise.

So "numpad:mac" option just tries to hide internal NumLock state but doesn't disable it.
Is there another way to force numpad to act as NumLock is on? I tried to dump xkb state with xkbcomp, change Level2 values to same as Level1 for NumLock modifier and load it back, but it didn't work.
Maybe someone already have some xkbcomp preset with NumLock permanently enabled?

Offline

#2 2021-07-20 20:52:21

seth
Member
Registered: 2012-09-03
Posts: 22,172

Re: Set NumLock always on in Xorg

Does it work (with a curses client) in xterm?
(See eg. https://codeberg.org/dnkl/foot/issues/194 for a discussion of the matter)

Offline

#3 2021-07-21 08:34:29

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 67

Re: Set NumLock always on in Xorg

seth wrote:

Does it work (with a curses client) in xterm?

The same behaviour in xterm and urxvt. Terminator and tilda (both vte3-based) work fine.

seth wrote:

(See eg. https://codeberg.org/dnkl/foot/issues/194 for a discussion of the matter)

Thanks. Will try to figure it out.

Offline

#4 2021-07-21 16:16:38

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 67

Re: Set NumLock always on in Xorg

seth wrote:

(See eg. https://codeberg.org/dnkl/foot/issues/194 for a discussion of the matter)

That topic is more about ncurses handling of numpad keys. My goal is to disable NumLock switching off at all on xorg level. I searched this guestion on different forums and all i found is suggestions to set "numpad:mac", but it's not what i need. Invalid ncurses behavior is just a consequence that xorg doesn't actually set single mode for numpad, it only translates a key in both modes to the same character, but two modes still exist and they're distinguishable.
As i understand, first what i have to do is to disable internal xorg NumLock handling, numpad keys in this case should act as arrows, home/end etc. Second is to found and replace arrows, home/end etc key codes to digit codes as for regular digit keys.

Last edited by dimich (2021-07-21 16:17:45)

Offline

#5 2021-07-21 19:57:27

seth
Member
Registered: 2012-09-03
Posts: 22,172

Re: Set NumLock always on in Xorg

My goal is to disable NumLock switching off at all on xorg level.

This is probably not going to work since I assume your actual goal is to use the status LED as layout indicator and I also assume the layout switching is also what toggles it.
Afaik the LED is hard-bound to the state and afaiu it's the state that bothers you w/ ncurses - not the input behavior that produces the desired eg. KP_1 already.

I don't think that you'll be able to toggle the LED w/o toggling the state (at least not w/o manipulating the xserver code) so you will have to focus on the client side.

Offline

#6 2021-07-21 20:37:44

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 67

Re: Set NumLock always on in Xorg

seth wrote:

I assume your actual goal is to use the status LED as layout indicator

Actually not. NumLock LED works fine as layout indicator even with current configuration, NumLock key press doesn't affect it. I monitor internal state of NumLock modifier with xkbwatch.

Offline

#7 2021-07-21 20:43:15

seth
Member
Registered: 2012-09-03
Posts: 22,172

Re: Set NumLock always on in Xorg

seth wrote:

I assume your actual goal is to use the status LED as layout indicator

dimich wrote:

Actually not.

Then what's the point of "grp_led:num"?

NumLock LED works fine as layout indicator even with current configuration

So your actual goal is to use it as that indicator…?!

Not the goal of this thread, ie. a problem you're trying to solve. But the general idea: what you're trying to do and what ultimately caused the situation discussed in this thread
Edit: what in essence is to decouple the LED from the state.

Last edited by seth (2021-07-21 20:45:02)

Offline

#8 2021-07-22 10:22:56

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 67

Re: Set NumLock always on in Xorg

seth wrote:

Then what's the point of "grp_led:num"?
So your actual goal is to use it as that indicator…?!

I already use it as indicator. But it doesn't affect numpad keys and NumLock key behavior. Even if remove grp_led from XkbOptions, numpad keys continue to change their meaning in some applications after NumLock key pressed.
Initial reason is the following: i often enter digital data, e.g. IP addresses etc, and if NumLock was pressed accidentally (e.g. a cat walked on keyboard, even if keyboard is locked) it doesn't do what i expect. It's very annoying. So i wondered if it's possible to "lock NumLock permanently". All i found on Internet is "numpad:mac", but it doesn't work for minicom i often use, and some other applications. Then i wondered how easy it is to customize keyboard behavior in xorg. If NumLock key is useless (for me), why i can't just disable it and customize keyboard to my purposes. *nix is flexible and customizable system, isn't it?
Of course, it's possible to remove NumLock key physically, but i think it's barbarism smile BTW, maybe it's possible to switch numlock state on with numlockx and disable NumLock key with xmodmap, i should try it. Anyway, it would be just a workaround.

Offline

#9 2021-07-22 12:23:24

Lone_Wolf
Member
From: Netherlands, Europe
Registered: 2005-10-04
Posts: 9,336

Re: Set NumLock always on in Xorg

dimich wrote:

BTW, maybe it's possible to switch numlock state on with numlockx and disable NumLock key with xmodmap, i should try it. Anyway, it would be just a workaround.


It does work with xmodmap .

While num_lock on I tried

$ xmodmap -pke | grep Num_Lock
keycode  77 = Num_Lock NoSymbol Num_Lock
$ xmodmap -e "keycode 77 = NoSymbol NoSymbol NoSymbol"

A quick test showed the numeric pad still worked in terminal , but nothing happened when pressing num_lock key .

I used xmodmap -e "keycode 77 = Num_Lock NoSymbol Num_Lock" to revert to normal .

Last edited by Lone_Wolf (2021-07-22 12:24:47)


Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.
Did you use the guided installer ? If yes, I can't help you.

(A works at time B)  && (time C > time B ) ≠  (A works at time C)

Offline

#10 2021-07-22 14:37:49

seth
Member
Registered: 2012-09-03
Posts: 22,172

Re: Set NumLock always on in Xorg

if NumLock was pressed accidentally

My understanding was the the state undesiredly changes because you're using the indicator for the layout switch.

Deactivating or functioning away the numpad key is of course possible, but the state will align to the LED which in your setup aligns to the layout.
If that's not the case, xmodmap'ing it away as in #9 is gonna be the least resisting approach.

Offline

#11 2021-07-22 18:22:59

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 67

Re: Set NumLock always on in Xorg

Lone_Wolf wrote:

I used xmodmap -e "keycode 77 = Num_Lock NoSymbol Num_Lock" to revert to normal .

Yep, xmodmap works. But numlockx must be run first, otherwise it returns error:

X Error of failed request:  BadValue (integer parameter out of range for operation)
  Major opcode of failed request:  132 (XTEST)
  Minor opcode of failed request:  2 (X_XTestFakeInput)
  Value in failed request:  0x0
  Serial number of failed request:  17
  Current serial number in output stream:  20

On the other hand, invoking numlockx first makes race condition: if NumLock is pressed after numlockx but before xmodmap, it will stay turned off smile Very unlikely but possible.

Offline

#12 2021-07-22 20:08:53

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 67

Re: Set NumLock always on in Xorg

dimich wrote:

Yep, xmodmap works.

Weird thing: numlockx+xmodmap works for hexedit but doesn't work for minicom.

Offline

#13 2021-07-23 06:32:54

seth
Member
Registered: 2012-09-03
Posts: 22,172

Re: Set NumLock always on in Xorg

Instead of mapping the key away, try removing the modifer.

xmodmap -pm
xmodmap -e 'clear mod2' # assuming that mod2 is Num_Lock

nb. that client behavior can differ depending on whether the client started before or after the change.

Offline

Board footer

Powered by FluxBB