Ubuntu – Modeless/stateless layout language switching with Caps Lock, again (18.04 LTS Bionic Beaver)

18.04accessibilitygnomeinput-languagekeyboard-layout

This has been asked countless times, so I'll try to be brief.

I have two input layouts, Ukrainian and English, and want:

  • to use Caps Lock for fast switching;
  • not have any delays imposed, I'm a fast typer;
  • not have mode errors:
    • (any number of) CapsLock sets English,
    • (any number of) ShiftCapsLock sets Ukrainian;
  • see the current layout in the standard widget.

I've already tried three (!) GUIs to configure this, the Gnome Settings, the gnome-tweak-tool, the dconf-editor/gsettings. Nothing works. Tweaks crashes randomly.

Enough homework done

I've asked this myself 4 years ago for 14.04 LTS. Since then, that UI popup appeared and all broke. I can now easily configure stateful CapsLock toggle, but even then, it goes with that irritating delay. Basically unusable at times when I need language switched 4-5 times per sentence.

This thread suggests caps:none XKB option as a bug workaround. I've tried that, both with gsettings CLI and dconf-editor, and in combinations with grp:shift_caps_switch XKB option. No dice. Something trumps those core XKB features as it seems.

This thread offers caps:menu XKB option as a hack to be able to bind CapsLock in Gnome Settings → Devices → Keyboard. It kinda works, but A) it gives cyclic/stateful switching; B) I have to wait a decent timeout on every switch.

This thread asks exactly what I need, yet it's unanswered and even worse: closed as duplicate of two other questions (???) none of which are the same, and none of which give me a working solution. What's wrong with you guys?

There's another exact match — alas, unanswered as well!


Wanted: clean solution

Best Answer

In Ubuntu 18.04, by default there is a option to choose Caps_Lock and Shift+Caps_Lock to achieve the behavior we are looking for, is in gnome-tweaks. But this is not working and could not get the reason why.

enter image description here

I could manage to achieve this behavior by the below workaround.

See this link for a 20Sec Video. https://imgur.com/QNdrVqa

first check the below commands in terminal whether they are changing the input language.

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

and

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()"

well, they are working.

enter image description here

we need to bind these commands to key presses of Caps_Lock and Shift+Caps_Lock

can achieve this with xbindkeys if xbindkeys is not yet installed install it by sudo apt install xbindkeys

  1. create a file .xbindkeysrc in $HOME folder
    touch ~/.xbindkeysrc
  2. to know the keycode/id what ever it is called
    run xbindkeys --key once the white box appears press the Caps_Lock button, do it for Shift button also and copy the keycodes/ids as show below

enter image description here

  1. this step is Optional.. Disable the Caps Lock. See below gif. If caps lock is not disabled.. when you hit the key multiple times it will toggle while you try to change the language. I have Disabled it. but it is based on your interest.

enter image description here

  1. Open the file .xbindkeysrc
    gedit ~/.xbindkeysrc and edit the file like below based on the results of above steps

Change the values in below content according to the results you got in step 2 above. Content:

"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()""
m:0x10 + c:66

"gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval "imports.ui.status.keyboard.getInputSourceManager().inputSources[1].activate()""
Shift+Mod2 + Shift_L+m:0x10 + c:66
  1. Save the file, Close & Reboot..