Toggling between Japanese input modes in Ubuntu with a non-Japanese keyboard

Context

Japanese input with Mozc can be annoying in Ubuntu when you don't have a Japanese keyboard with the expected special keys for toggling between different input modes, most importantly the non-IME (Input Method Editor) mode "Direct input" and IME modes like "Hiragana".

When I use Japanese input, I'm mainly interested in toggling between direct input for e.g. writing terminal commands or generally something in English, and hiragana input for writing Japanese that can then be converted. Toggling between say hiragana and katakana never seemed very important to me since I can usually rely on the IME conversions for getting the right Japanese characters. But by default in Ubuntu, even if you only want to toggle between IME and non-IME input, you need to click through a couple of drop-downs at the top right of the screen (clicking the language symbol -> Input Mode -> <Direct input / Hiragana>) unless you have a Japanese keyboard with kana keys which you can use as shortcuts.

Steps for adding shortcuts

There are many guides that explain well how to install and enable Japanese Mozc input, like this one from Moritz Molch. Instead of repeating those instructions, I'll assume that you already have Japanese Mozc input set up and briefly outline the steps I follow to get the keyboard shortcuts I want in place.

  1. Select Japanese (Mozc) as your input source if you haven't already.
  2. Click the input source icon at the top right.
  3. Select 'Tools' in the dropdown.
  4. Select 'Properties'.
  5. In the 'Mozc Settings' window, under 'Keymap' and next to 'Keymap style', click 'Customize...'.
  6. In the 'Mozc keymap editor' window, click 'Edit' down in the bottom left.
  7. In the dropdown, select 'New entry'.

You'll get a new entry in the keymap table. Insert the following (by double-clicking each cell):

  • Mode: Direct input
  • Key: Ctrl + Shift + Space
    • Or whatever key combination you prefer.
  • Command: Activate IME

Now repeat the process to add another new entry with the following entries:

  • Mode: Precomposition
  • Key: Ctrl + Shift + Space
  • Command: Deactivate IME

If, like me, you used 'Ctrl + Shift + Space' as the key combination, you should also search in the table for an entry with a Precomposition command 'Insert full-width space' and remove it (right click -> remove entry), since it uses the same key combination by default (at least in my version it did).

Now hit 'OK' twice to save your changes and exit the Mozc settings, log out and then log back in to ensure that your updates are properly applied. Open up a text document and try using the key combination you chose ('Ctrl + Shift + Space') to toggle between IME and non-IME input. If you find that the combination doesn't work, you might have to try another - I found that, for whatever reason, some key combinations just didn't work, like 'Ctrl + Shift + y', before I settled on 'Ctrl + Shift + Space'.

Bonus: Explicitly set the Direct Input keyboard layout

I always have Swedish as a secondary input source. This means that even if I put Japanese (Mozc) as the primary input source, it uses the Swedish keyboard layout when in the 'Direct Input' mode, but I prefer using an US English keyboard layout e.g. when coding. As described in this SO thread, it's possible to explicitly tell Mozc what keyboard layout to use rather than having it fall back to whatever other input source comes first, by editing /usr/share/ibus/component/mozc.xml. In my case, I changed the line reading <layout>default</layout> to <layout>us</layout>.

Update: In Ubuntu 22, I found that the method for setting the keyboard layout had changed slightly. When I checked the "mozc.xml" file, it had a comment that pointed me to $HOME/.config/mozc/ibus_config.textproto, where I needed to change the line layout : "default" to layout : "us". I then ran ibus write-cache; ibus restart, and logged out/in.