Ubuntu – Emulate Numpad in linux

keyboardnumpad

My laptop does not have a numpad, not even those in which it is activated by the FN key.

When I was using windows, I used AutoHotkey with a script that I replaced the QWEASDZXC keys by 789456123, and if I want to disable it just need to type F12.

Is there a way to do it in linux?

Best Answer

I can think of a couple ways to do this with XKB, but it's not for the faint of heart.

  1. Make a custom symbol option that adds your numbers to "level3" or "level5" of your chosen keys. (AltGr is the ISO_Level3_Shift key, used in non-US or US-intl layouts for áçćéñþëd characters.) Normally these keys act like Shift -- you'd have to hold them down while you typed the numbers. But your new option could make them act more like CapsLock if preferred. Such an option would then override your layout's default bindings for those letters to add the numbers.

    // emulate numpad on first 3 columns of alphabetic keys
    // initial key definitions from /usr/share/X11/xkb/symbols/us
    // ("intl" stanza)
    partial 
    xkb_symbols "qweasdzxc" {
    
        // numbers on level3 (RightAlt chooses lv3)
        include "level3(ralt_switch)"
        key <AD01> { [ q, Q, 7, division ] };
        key <AD02> { [ w, W, 8, multiply ] };
        key <AD03> { [ e, E, 9,    minus ] };
        // etc ...
    
        // OR ...
    
        // numbers on level5 (RightCtrl chooses lv5)
        include "level5(rctrl_switch)"
        key <AD01> { [ q, Q, adiaeresis,  Adiaeresis,  7,  division ] };
        key <AD02> { [ w, W,      aring,       Aring,  8,  multiply ] };
        key <AD03> { [ e, E,     eacute,      Eacute,  9,     minus ] };
        // etc ...
    
    }; // end "qweasdzxc"
    
    
    // rules to load this as an option
    ! option         = symbols
      lv3:qweasdzxc  = +filename(qweasdzxc)
    
    
    // load from commandline. may need -I/path/to/custom/xkb
    setxkbmap -layout us -option lv3:qweasdzxc
    
  2. Make an empty layout that emits only those numbers on those keys and no other alphanumeric symbols. When loaded in as a second layout, you'd have a predefined layout-switch key and would use that to toggle between your main layout and this one. Again, the layout-switch key could be configured like a Shift (only switch layouts while held) or a CapsLock.

    // emulate numpad on first 3 columns of alphabetic keys
    // leave other alphanumeric keys undefined
    // otherwise from /usr/share/X11/xkb/symbols/us (basic stanza)
    default partial alphanumeric_keys modifier_keys
    xkb_symbols "qweasdzxc" {
    
        name[Group1]= "Numpad Emulation";
    
        key <AD01> { [ 7, division ] }; // q
        key <AD02> { [ 8, multiply ] }; // w
        key <AD03> { [ 9,    minus ] }; // e
    
        key <AC01> { [ 4,        F ] }; // a
        key <AC02> { [ 5,        E ] }; // s
        key <AC03> { [ 6,        D ] }; // d
    
        key <AB01> { [ 1,        C ] }; // z
        key <AB02> { [ 2,        B ] }; // x
        key <AB03> { [ 3,        A ] }; // c
        // hexadecimal just for fun, replace if desired
    
    }; // end "qweasdzxc"
    
    
    // rules to load this as a layout
    ! layout         = symbols
      qweasdzxc      = qweasdzxc
    
    
    // load from commandline. may need -I/path/to/custom/xkb
    setxkbmap -layout us,qweasdzxc -option grp:caps_toggle
    

Either of these options can be implemented by altering system XKB files in /usr/share/X11/xkb/{symbols,rules} or by storing your customizations in local XKB configuration files. Changes to the system files could be wiped out by an update to the xkeyboard-config package, but are easier to use with system layout setting tools like /etc/default/keyboard or GNOME's settings daemon.