2

I'm trying to change Ubuntu 20.04's behavior in the following way: I want Left Alt to switch keyboard layout to English and Right Alt to switch it to Russian. I also want to keep all my Alt+smth hotkeys.

What I figured out so far:

  • custom hotkeys: Ubuntu settings or Tweak Tools can't do what I want. I can use xbindkeys to bind Alt_L and Alt_R to any shell command but it breaks Alt+smth hotkeys even if I use Release modifier (like Alt_L + Release). I still don't know how to map anything to Alt without breaking Alt+smth.
  • speaking of shell commands, the best I could do to change keyboard layout with a shell command is setxkbmap us or setxkbmap ru. This kinda works but ubuntu somehow doesn't see this: when I press the switch language hotkey the keyboard layout changes in ubuntu tray but actual layout is always us. Also setxkbmap us doesn't actully change the currently selected layout, it limits the list of available layouts to just us instead of us,ru which is a workaround but I could live with that I guess.

So the question is: how can I bind Left Alt to toggle US layout and Right Alt to toggle RU layout while keeping all Alt+smth hotkeys?

4
  • Welcome to AskUbuntu, while Alt_L + key is bind to something (likely so); would be annoying, if you could assign that key. The thing is simple if you try to set a hotkey in the System Settings > Keyboard > Shortcuts, probably the Switch to next source is set to super + space try to change it for your favorite key combination. Jan 14 at 11:16
  • @guiverc thanks for the clarification, I meant ubuntu 20.04, edited the question Jan 14 at 13:50
  • @SadaharuWakisaka thanks for your answer. The point is that I've used this setup on Mac before and I've developed the muscle memory to press left or right alt with my thumb to switch to the language I'm going to type in before I start typing. So it wouldn't be annoying for me if both Alt and Alt + key hotkeys are set. Also I want to bind keys to select a specific layout rather than to switch layouts. Do you know if this is possible? Jan 14 at 13:56
  • Some inspiration here: unix.stackexchange.com/questions/316998/…
    – vanadium
    Jan 14 at 15:26
3

I figured out how to do it using @vanadium 's comment and dconf-tool.

Switching language from terminal using gnome shell's eval:

gdbus call --session --dest org.gnome.Shell \
--object-path /org/gnome/Shell \
--method org.gnome.Shell.Eval \
"imports.ui.status.keyboard.getInputSourceManager().inputSources[0].activate()"

Change the index of inputSources for different languages.

To set a hotkey you need dconf-tool. Navigate to /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/. Conveniently you can't add new subfolders from dconf-editor interface, go to Settings -> Keyboard Shortcuts, scroll down to Custom Shortcuts and add a new one. custom0 subfolder should appear in dconf-tool's interface. Set the command above as command and Alt_L or Alt_R as binding.

0

I wasn't able to assign desired hotkeys to Alt_L / Alt_r and keep other Alt+smth hotkeys working, but I was able to achieve requested functionality with less conflicting keys in i3wm, so my answer could be partially useful.

I added the following lines to the .config/i3/config:

bindcode Mod4+57 exec "setxkbmap -layout us,ru"
bindcode Mod4+58 exec "setxkbmap -layout ru,us"

Now windows_key + n (n has keycode 57) and windows_key + m (m has keycode 58) switch the layout to English and Russian respectively.

Note that it's required to use bindcode together with keycodes instead of bindsym together with symbols in config, because otherwise the hotkeys won't work in Russian layout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.