Switch keyboard layouts programmatically!
It’s a shame that there is no easy way to switch keyboard input layout programmatically in Ubuntu (GNOME). All the existing solutions are flawed in some way or another:
-
gsettings set org.gnome.desktop.input-sources …
, found in many answers in the Internet is not working anymore in the latest Ubuntu versions. -
setxkbmap …
doesn’t play well with the GNOME Shell. It switches the layout, but the UI doesn’t reflect the change and you cannot switch languages with the keyboard shortcut anymore untill you restart. -
The
gdbus call … method org.gnome.Shell.Eval
is deprecated & disabled due to the security reasons with theEval
method. -
Finally, the
org.gnome.Shell.Shell.Eval
replacement is really sketchy, and it’s essentially the same powerfulrEval
but from a third-party dev from GitHub.
Shyriiwook is a GNOME Shell extension that allows to switch keyboard layouts programmatically.
The extension exposes a D-Bus interface, that could be used to query for the available layouts and to switch between them via gdbus call …
.
However, it is programmed to only switch between the layouts, and does not expose a security breach like the Eval
method.
First, install the extension. You could find it in the GNOME Extensions. Just follow the instructions.
Alternatively, you could use the Extension Manager app to find Shyriiwook
and install it from there.
Finally, you could build the extension from the source code and install it manually.
After the extension is installed, you could use the gdbus
tool to query for the available layouts and to switch between them.
To query for the available layouts, run:
gdbus introspect --session --dest org.gnome.Shell --object-path /me/madhead/Shyriiwook --only-properties
It will output something like this:
node /me/madhead/Shyriiwook {
interface me.madhead.Shyriiwook {
properties:
readonly as availableLayouts = ['us', 'by+ru'];
readonly s currentLayout = 'us';
};
};
These availableLayouts
could be used to switch between the layouts, just pass one of them as an argument to the activate
method:
gdbus call --session --dest org.gnome.Shell --object-path /me/madhead/Shyriiwook --method me.madhead.Shyriiwook.activate "by+ru"
The currentLayout
property, obviously, contains the current layout.
That’s it!
Now you could use those in your scripts, or even bind it to a keyboard shortcut in Gnome settings.
To do that, add a new custom shortcut with the dbus call …
command under the "Settings" → "Keyboard" → "Keyboard Shortcuts" → "View and Customise Shortcuts" → "Custom Shortcuts".
First, determine your GNOME version. Starting from GNOME 45 some of the APIs used by the extension have changed, so you need to build the extension with the correct version.
To build the extension, you need to have the Make build tool installed.
Then, just run:
make 42 # To build for the GNOME 42
make 45 # To build for the GNOME 45
The build
directory will contain the extension in a corresponding subdirectory.
To build the extension ZIP archive, run:
make extension-42
make extension-45
If you want to tinker with the extension, you could symlink the directory to the extensions directory:
ln -s $(pwd)/42 ~/.local/share/gnome-shell/extensions/[email protected]
To build and install the extension, you could run
make install