Kodi Community Forum

Full Version: Guide: How to map your remote's "right click" to an action (by making it a key)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a shiny new remote control for Kodi. It comes with a built-in mouse, which I disable, but while the left-click button turns into the Enter key, the right-click button remains.

It is apparently very difficult to map a mouse button to an action in Kodi. In another thread, it is revealed that a keymap for <mouse><rightclick>…</rightclick></mouse> won't work, presumably because there is no mouse position when the mouse is disabled. My solution to this was to get Linux to map it to a key and then give that key an action in Kodi's keymap. (It would be nice if kodi could map mouse buttons as if they were keys without all of this extra work.)

There is some extra set-up to do first (please let me know if there's a better way do to this, this seems circuitous):

First, we need to create a new launcher that wraps around kodi. I use a kodi standalone session in lightdm, so for me that required editing /usr/share/xsessions/kodi.desktop to change kodi-standalone to kodi-standalone-plus and then create the following in /usr/local/bin/kodi-standalone-plus:
bash:
#!/bin/sh
# kodi-standalone-plus by watch, https://forum.kodi.tv/showthread.php?tid=335312
#
# Run all executable scripts in ~/.kodi/plus (see `man run-parts`) then run kodi

if [ -d "$HOME/.kodi/plus" ] && command -v run-parts >/dev/null >&2; then
  run-parts --report --exit-on-error "$HOME/.kodi/plus"
fi

kodi-standalone

This checks for the presence of a ~/.kodi/plus/ directory (which you must create) and ensures you have the run-parts command. If you do, it then runs all executable scripts in that directory before starting the real kodi-standalone command. Make sure this new script is executable too: sudo chmod +x /usr/local/bin/kodi-standalone-plus

(It would be nice if Kodi had the above functionality built in, which might work better with its auto-restart loop for crash recovery.)

For the rest, you'll need to install xbindkeys and xautomation (for xte). In Ubuntu, that's just sudo apt-get install xbindkeys xautomation

Now that you can execute arbitrary scripts right before Kodi, let's create one, say ~/.kodi/plus/20_xbindkeys with content:
bash:
#!/bin/sh

RC="$HOME/.xbindkeysrc-kodi"
if command -v xbindkeys xte >/dev/null 2>&1 && [ -r "$RC" ]; then
  xbindkeys -f "$RC" &
fi

Okay. More checks for commands, a check to ensure there is a rc file, then we execute xbindkeys. Let's make that rc file:
bash:
# Bind the right mouse button to "Back"
"xte 'key XF86Back'"
  b:3

This launches the shell command xte 'key XF86Back' which hits the Back key. It triggers on mouse button 3 (right click).

You can choose any key you like (consider any XF86 keyboard symbol for example) and then map it within Kodi as needed.
According to a 5+ year old thread on How to execute a command while starting XBMC, the "right" way to launch things is with ~/.kodi/userdata/autoexec.py, but that does not work for me. (Perhaps it's not run with the right environment? I did try xbindkeys -X :0 to explicitly state the display, so that's not it.)