The problem with January's solution is that it doesn't play nicely with Unity's indicator.
Below is a small python script that you can use instead of setxkbmap:
#!/usr/bin/env python
import argparse
from gi.repository import Gio
setting = Gio.Settings.new("org.gnome.desktop.input-sources")
keymaps = [keymap for (_, keymap) in setting['sources']]
parser = argparse.ArgumentParser(prog='keymap_cycle', description='Cycle through a subset of enabled keymaps.')
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument('-s', '--show', action='store_true', help='show available keymaps')
group.add_argument('-k', '--keymaps', default=','.join(keymaps), help='cycle through keymaps (default: %(default)s)')
arguments = parser.parse_args()
if arguments.show:
print 'Available keymaps: %s' % ', '.join(keymaps)
else:
cycle = arguments.keymaps.split(',')
current_language = keymaps[setting['current']]
try:
current_cycle_index = cycle.index(current_language)
desired_cycle_index = (current_cycle_index + 1) % len(cycle)
except ValueError:
desired_cycle_index = 0
desired_language = cycle[desired_cycle_index]
try:
desired_language_index = keymaps.index(desired_language)
setting['current'] = desired_language_index
except ValueError:
pass
Save it to $HOME/.local/bin
(or whatever other place you like), then chmod +x
it.
Add all the keymaps you want via standard Ubuntu's keymap configuration screen.
Then the original question could be solved by binding a shortcut for each of the following commands:
keymap_cycle --keymap en
keymap_cycle --keymap pt
keymap_cycle --keymap it
But it can do even more! For example keymap_cycle --keymap en,it
would cycle keymaps like this en->it->en->it... on each invocation.
To show all available keymaps you have added via Ubuntu's keymap configuration screen run keymap_cycle --show
.
Kudos to @bjonen and his answer for the gsettings magic insight.