4

After trying several Space and AltGr key remapping ideas, changing xkb looks most promising. I read and used these ideas related to xkb:
Remap altgr key to AC10 in XKB
How can I remap keyboard keys?

From the first glance my corrections seem to work in text editor and in the terminal(Space symbol is printed on AltGr key press). However, in Firefox it does not work. Looking more closely, I have noticed that in all the three applications key combinations with AltGr also work (as if no remapping was done), except that in the terminal and in text editor the combination additionally prints the space character. E.g., in the terminal and in FireFox the combination AltGr + F opens the File menu and in the terminal also a space character is printed.

My Space is broken and I don't use AltGr. I just want to completely swap their functionality. What am I missing? (I use Kali Linux.)

This is what I did:
1. Opened /usr/share/X11/xkb/symbols/pc
2. Changed the commented line with the one below:

 // key <SPCE> {    [    space      ]   };
 key <SPCE> {   [ ISO_Level3_Shift  ]   };

3. Added these lines:

// Additionally added
key <RALT> {    [    32     ]   };

4. Saved the pc file.
5. Cleared the xkb cache running rm -rf /var/lib/xkb/*
6. Restarted


Additional information (after the alterations listed above).

Full current pc file:

default  partial alphanumeric_keys modifier_keys
xkb_symbols "pc105" {

    key <ESC>  {    [ Escape        ]   };

    // The extra key on many European keyboards:
    key <LSGT> {    [ less, greater, bar, brokenbar ] };

    // The following keys are common to all layouts.
    key <BKSL> {    [ backslash,    bar ]   };
    // key <SPCE> { [    space      ]   };
    key <SPCE> {    [ ISO_Level3_Shift  ]   };

    include "srvr_ctrl(fkey2vt)"
    include "pc(editing)"
    include "keypad(x11)"

    key <BKSP> {    [ BackSpace, BackSpace  ]   };

    key  <TAB> {    [ Tab,  ISO_Left_Tab    ]   };
    key <RTRN> {    [ Return        ]   };

    key <CAPS> {    [ Caps_Lock     ]   };
    key <NMLK> {    [ Num_Lock      ]   };

    key <LFSH> {    [ Shift_L       ]   };
    key <LCTL> {    [ Control_L     ]   };
    key <LWIN> {    [ Super_L       ]   };

    key <RTSH> {    [ Shift_R       ]   };
    key <RCTL> {    [ Control_R     ]   };
    key <RWIN> {    [ Super_R       ]   };
    key <MENU> {    [ Menu          ]   };

    // Beginning of modifier mappings.
    modifier_map Shift  { Shift_L, Shift_R };
    modifier_map Lock   { Caps_Lock };
    modifier_map Control{ Control_L, Control_R };
    modifier_map Mod2   { Num_Lock };
    modifier_map Mod4   { Super_L, Super_R };

    // Fake keys for virtual<->real modifiers mapping:
    key <LVL3> {    [ ISO_Level3_Shift  ]   };
    key <MDSW> {    [ Mode_switch       ]   };
    modifier_map Mod5   { <LVL3>, <MDSW> };

    key <ALT>  {    [ NoSymbol, Alt_L   ]   };
    include "altwin(meta_alt)"

    key <META> {    [ NoSymbol, Meta_L  ]   };
    modifier_map Mod1   { <META> };

    key <SUPR> {    [ NoSymbol, Super_L ]   };
    modifier_map Mod4   { <SUPR> };

    key <HYPR> {    [ NoSymbol, Hyper_L ]   };
    modifier_map Mod4   { <HYPR> };
    // End of modifier mappings.

    key <OUTP> { [ XF86Display ] };
    key <KITG> { [ XF86KbdLightOnOff ] };
    key <KIDN> { [ XF86KbdBrightnessDown ] };
    key <KIUP> { [ XF86KbdBrightnessUp ] };

    // Additionally added
    key <RALT> {    [    32     ]   };

};

hidden partial alphanumeric_keys
xkb_symbols "editing" {
    key <PRSC> {
    type= "PC_ALT_LEVEL2",
    symbols[Group1]= [ Print, Sys_Req ]
    };
    key <SCLK> {    [  Scroll_Lock      ]   };
    key <PAUS> {
    type= "PC_CONTROL_LEVEL2",
    symbols[Group1]= [ Pause, Break ]
    };
    key  <INS> {    [  Insert       ]   };
    key <HOME> {    [  Home         ]   };
    key <PGUP> {    [  Prior        ]   };
    key <DELE> {    [  Delete       ]   };
    key  <END> {    [  End          ]   };
    key <PGDN> {    [  Next         ]   };

    key   <UP> {    [  Up           ]   };
    key <LEFT> {    [  Left         ]   };
    key <DOWN> {    [  Down         ]   };
    key <RGHT> {    [  Right        ]   };
};

setxkbmap -print:

xkb_keymap {
    xkb_keycodes  { include "evdev+aliases(qwerty)" };
    xkb_types     { include "complete"  };
    xkb_compat    { include "complete"  };
    xkb_symbols   { include "pc+us+inet(evdev)" };
    xkb_geometry  { include "pc(pc105)" };
};

xev (AltGr press and release):

KeyPress event, serial 34, synthetic NO, window 0x3400001,
    root 0x6cb, subw 0x0, time 1249191, (168,-8), root:(797,298),
    state 0x0, keycode 108 (keysym 0x20, space), same_screen YES,
    XLookupString gives 1 bytes: (20) " "
    XmbLookupString gives 1 bytes: (20) " "
    XFilterEvent returns: False

KeyRelease event, serial 37, synthetic NO, window 0x3400001,
    root 0x6cb, subw 0x0, time 1249261, (168,-8), root:(797,298),
    state 0x8, keycode 108 (keysym 0x20, space), same_screen YES,
    XLookupString gives 1 bytes: (20) " "
    XFilterEvent returns: False

As my keyboard's Space key is broken, I cannot show its xev.

0

2 Answers 2

3

I've actually made it work even with xkeymap, I leave it here as a different answer because the method is strictly different from my previous solution.

Maybe you've missed some occurrence of AltGr usage.

Trying to mimic your setup, I've exported the current configuration file with the command xkbcomp $DISPLAY current.xkb and edited it by removing all ISO_Level3_Shift, LVL3 and Mod5 occurrences.

Precisely, what I've deleted is:

interpret ISO_Level3_Shift+AnyOf(all) {
    virtualModifier= LevelThree;
    useModMapMods=level1;
    action= SetMods(modifiers=LevelThree,clearLocks);
};
interpret ISO_Level3_Shift+AnyOfOrNone(all) {
    action= SetMods(modifiers=LevelThree,clearLocks);
};
key <LVL3> {
    type= "ONE_LEVEL",
    symbols[Group1]= [ ISO_Level3_Shift ]
};
key <RALT> {
    type= "ONE_LEVEL",
    symbols[Group1]= [ ISO_Level3_Shift ]
};
modifier_map Mod5 { <LVL3> };

Then I've inserted:

key <LVL3> {[space]};
key <RALT> {[space]};

This edited configuration can be tested by loading it with

xkbcomp new_conf.xkb $DISPLAY
2
  • As xev's output show, although the keysym was remapped successfully, the AltGr key retains its modifier property, possibly confusing some applications. This solution removes the modifier property. Very nice, +1.
    – Quasímodo
    Jan 7, 2021 at 22:03
  • I have not tested this answer myself, as your other answer worked brilliantly. However, I will accept this one as correct, because my original question was about xkb.
    – ZygD
    Jan 8, 2021 at 18:15
1
+50

I did not find any reference that proves that what you've proposed should not work on Firefox, while it seems that Chromium (and therefore Chrome) does not relies on xkeymaps: it is a developing choice that better integrates the program in ChromeOS. Some system remapping can therefore be not supported.

A fix that worked for me is to remap keycodes at kernel level. To achieve this, I've collected scan-codes (the hardware ID of the key) and key-codes (the symbol in which they are mapped) for both space and altGr:

# to collect scan-codes
sudo showkey -s
# space press: 0x39
# altGr press: 0xe0 0x38

# to collect key-codes
sudo showkey
# space press: 57
# altGr press: 100

As far as I understood, you'd like to remap the altGr to space because your spacebar is broken, therefore:

sudo setkeycodes e038 57

should do the trick.


In order to make this permanent under Kali Linux, you have to edit the default xinit file, /etc/xdg/xfce4/xinitrc, by coping it to ~/.xinitrc and inserting sudo setkeycodes e038 57 before the if statement that contains exec xfce4-session; on Kali 20.04 default file, this if statement is on line 86.

Note that this requires to enable sudo without password), but as far as I can recall, this is already the default option in Kali.

5
  • @Maik93 - the suggestion worked, but I'm struggling with the last thing. It seems that my system does not have ~/.xinitrc. cat ~/.xinitrc returns No such file or directory. Find found 2 files xinitrc, but neither contained exec gnome-session.
    – ZygD
    Jan 7, 2021 at 19:58
  • Sorry, I saw Kali only when it was called Backtrack: at that time its Desktop Environment was Gnome. I see now that Kali is currently based on Xfce4, therefore you should copy its default configurations with cp /etc/xdg/xfce4/xinitrc ~/.xinitrc and edit the file generated in your home directory. In there, I can find exec xfce4-session (on line 102, inside an if test). I suggest you to insert the line above before that if statement, e.g. on line 86.
    – Maik93
    Jan 8, 2021 at 10:22
  • I strongly suggest updating your answer with the info from the last comment. I inserted the command in the /etc/xdg/xfce4/xinitrc and now the remap works after reboot.
    – ZygD
    Jan 8, 2021 at 18:10
  • Even though I have accepted the other answer as correct, I actually went for this option on my machine. So I want to grant this one the bounty.
    – ZygD
    Jan 8, 2021 at 18:17
  • 1
    Thank you, I'm glad to hear that this worked well for you. I've update my answer, but please note that any edits on the system file /etc/xdg/xfce4/xinitrc may be overwritten by xfce4 updates. It should be better to copy that file in ~/.xinitrc (note the dot in the destination file name).
    – Maik93
    Jan 9, 2021 at 16:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .