Score:2

Ubuntu 20.04 custom hotkeys to change keyboard layout

ph flag

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?

Sadaharu Wakisaka avatar
pl flag
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.
Artem Shalagin avatar
ph flag
@guiverc thanks for the clarification, I meant ubuntu 20.04, edited the question
Artem Shalagin avatar
ph flag
@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?
vanadium avatar
cn flag
Some inspiration here: https://unix.stackexchange.com/questions/316998/how-to-change-keyboard-layout-in-gnome-3-from-command-line
Score:3
ph flag

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.

Score:0
cn flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.