On X11 one way to achieve that is by using XKB and making the right alt key set the current latch mode as AltGr. There are a couple of ways to go about it:
Option 1
You can export your current keyboard layout to a file, change only the behavior of the right alt key (AltGr) and load it back to X, overriding the default layout:
Run xkbcomp $DISPLAY current.xkb
to export your current keyboard layout.
Open the current.xkb
file and locate the following snippet:
key <RALT> {
type= "ONE_LEVEL",
symbols[Group1]= [ ISO_Level3_Shift ]
};
Change it as follows:
key <RALT> {
type= "ONE_LEVEL",
symbols[Group1]= [ ISO_Level3_Shift ],
actions[Group1]= [ LatchMods(mods=AltGr) ]
};
Save the file and run xkb current.xkb $DISPLAY
to apply the new layout.
Caveats
The new layout is only good for the current X session. You'll probably want to reload it automatically after you log in. If you are using a window manager, just add xkb current.xkb $DISPLAY
to your .xinitrc
or .xprofile
. If you are using a desktop environment, it might be a little trickier because DEs usually handle keyboard layouts their own way and they can override local configurations. Be sure to check their docs, or try Option 2 below.
If you use multiple keyboard languages, you'll need to repeat the steps above for each exported xkb file and somehow load them when appropriate (e.g. using a shortcut for each language).
Option 2
You can create a new XKB option that enables the desired behavior for the AltGr key and then activate it along with a language layout:
Create a file in /usr/share/X11/xkb/symbols
named altgr
with the following content:
partial alphanumeric_keys
xkb_symbols "latch" {
key <RALT> {
symbols = [ ISO_Level3_Shift ],
actions = [ LatchMods(mods=AltGr) ]
};
};
Open the evdev
file in /usr/share/X11/xkb/rules
and locate the line ! option = symbols
. Add a new line after it and insert altgr:latch = +altgr(latch)
. It will look like the following:
! option = symbols
altgr:latch = +altgr(latch)
...
+altgr(latch)
means use the latch
group from the altgr
file located in /usr/share/X11/xkb/symbols
.
Now you need to activate the new option. You can do one of the following:
Run setxkbmap -layout us -variant altgr-intl -option altgr:latch
to activate the new option, replacing us
and altgr-intl
with your language and variant. This will last for the current X session (useful for testing) and you may want to add it to .xinitrc
or .xprofile
so it will load automatically on login.
If using systemd, run sudo localectl --no-convert set-x11-keymap us pc104 altgr-intl altgr:latch
so your keyboard configuration in /etc/X11/xorg.conf.d
is updated.
If using a desktop environment, there might be a way to tell it to activate the option when loading the keyboard layout. I know GNOME uses dconf and you can set options for XKB in org.gnome.desktop.input-sources.xkb-options
.
Caveat
XKB files in /usr/share/X11/xkb
aren't meant to be edited directly and might get overridden when performing system updates. The "right way" to change XKB configuration is way too complicated and I suggest you simply create a script that (re)applies the new option when needed.