HOW TO - Toggle hardware keyboard layout when Kodi is launched standalone
#1
Information 
This is an instruction on how to set up toggling of hardware (usb/radio/bluetooth) keyboard layouts when Kodi is launched as a standalone application. It's the case for kodi-centred distros like CoreELEC and LibreELEC. When launched standalone, there is no X-server that can handle HW KB layouts, so extra configuration is needed. I've tested this on CoreELEC (9.2, Kodi v18) with a bluetooth keyboard and toggling between 2 layouts.

The idea is taken from this post. We'll create a script to toggle layout via json rpc and bind it to the hotkey of your choice.

Step 1. Put the following script somewhere you find the best. I've put it into userdata/scripts; the full path on my system is /storage/.kodi/userdata/scripts/hwkb_cycle_layout.py, but it may be different for your distro.

python:

import xbmc
import json

LAYOUTS = ["us", "ru"]
KBLAYOUT_KEY = "input.libinputkeyboardlayout"


def rpc(method, params):
    payload = {
        "jsonrpc": "2.0",
        "method": method,
        "params": params,
        # Not sure what is this, but it's a required param
        "id": 1
    }
    response = json.loads(xbmc.executeJSONRPC(json.dumps(payload)))
    return response["result"]


def get_option(key):
    result = rpc("Settings.GetSettingValue", {"setting": key})
    return result["value"]

def set_option(key, value):
    return rpc("Settings.SetSettingValue", {"setting": key, "value": value})

def cycle_layout():
    layout = get_option(KBLAYOUT_KEY)
    next_layout = LAYOUTS[(LAYOUTS.index(layout) + 1) % len(LAYOUTS)]
    set_option(KBLAYOUT_KEY, next_layout)

if __name__ == "__main__":
    cycle_layout()


Step 2. Configure desired layouts. At the top of the script there are two constants; you'll need to tweak one that's called LAYOUTS. For example, if you want US English and German it should look like

python:

LAYOUTS = ["us", "de"]

Step 3. Bind a keyboard shortcut to the script. If you are already familiar with customising keymaps, just bind the following action (use the full path to the script):

python:
XBMC.RunScript(/storage/.kodi/userdata/scripts/hwkb_cycle_layout.py)

If it's the first time you'll be adding a custom hotkey, here is the example of how to bind it to Ctrl+Space (put this into userdata/keymaps/kblayout.xml):

xml:

<keymap>
  <global>
    <keyboard>
      <space mod="ctrl">XBMC.RunScript(/storage/.kodi/userdata/scripts/hwkb_cycle_layout.py)</space>
    </keyboard>
  </global>
</keymap>

Step 4. Reload hotkeys. I'll just restart Kodi for this purpose. If you know a better way - use it Smile

That's it - layout switching should work now. It won't show you that layout has changed (though you can enhance the script to show a popup via json rpc) and it won't sync to onscreen keyboard either. But you're now able to enter characters of all selected languages when you need it (e.g. for search feature).

2 Mods: could you please put it into Tips & Tricks section if it meets the standards, I've seen a few threads here (and in other kodi-related forums) asking how to switch layouts on hw keyboards.
Reply
#2
(2020-03-01, 14:50)residentsummer Wrote: ...
That's it - layout switching should work now. It won't show you that layout has changed (though you can enhance the script to show a popup via json rpc) and it won't sync to onscreen keyboard either. But you're now able to enter characters of all selected languages when you need it (e.g. for search feature).
...
hey @residentsummer, I'm using a similar script like I wrote at https://forum.kodi.tv/showthread.php?tid=352139, but sometimes (when switching languages quickly) Kodi freezes and restarts.
it also happens with using your script. do you see this behavior in your system as well?
Reply
#3
(2020-06-29, 09:17)shmizan Wrote:
(2020-03-01, 14:50)residentsummer Wrote: ...
That's it - layout switching should work now. It won't show you that layout has changed (though you can enhance the script to show a popup via json rpc) and it won't sync to onscreen keyboard either. But you're now able to enter characters of all selected languages when you need it (e.g. for search feature).
...
hey @residentsummer, I'm using a similar script like I wrote at https://forum.kodi.tv/showthread.php?tid=352139, but sometimes (when switching languages quickly) Kodi freezes and restarts.
it also happens with using your script. do you see this behavior in your system as well?

Hi, @shmizan . I don't have that box anymore, so can't check, sorry. Though, I do not remember any crashes when switching layouts (haven't tried to change 'em very quickly). Both scripts ain't doing anything "special" and crashes in scripts/addons do not usually crash Kodi, so my guess that it's some kind of race condition inside Kodi itself. If you're able to reproduce this behavior, I think it's worth posting an issue to a bug tracker.
Reply

Logout Mark Read Team Forum Stats Members Help
HOW TO - Toggle hardware keyboard layout when Kodi is launched standalone1