Skip to content

Instantly share code, notes, and snippets.

@BillyNate
Created June 25, 2017 13:49
Show Gist options
  • Select an option

  • Save BillyNate/a29091f1c44ed3b0be78295622d96cdd to your computer and use it in GitHub Desktop.

Select an option

Save BillyNate/a29091f1c44ed3b0be78295622d96cdd to your computer and use it in GitHub Desktop.
Send keystrokes to a remote Kodi eventserver
#!/usr/bin/env python
# This script takes keyboard input and uses it to send events to the Kodi eventserver,
# it can be installed on any small pc like a Pi.
# To use an ordinary remote one could a Flirc.
# (the eventserver will be started automaticly when Kodi starts)
# It is assumed the keyboard is at /dev/input/event0
# The IP address connected to need to be adjusted to your Kodi installation
# The XBMCClient class is used and can be found in the official Kodi repo
# To get the evdev library you'll need pip: apt-get install python-pip python dev
# and install the library: pip install evdev
from evdev import InputDevice, categorize, ecodes
from subprocess import call
import select
import time
from xbmcclient import XBMCClient
from xbmcclient import ACTION_BUTTON
client = XBMCClient(ip='192.168.x.x')
client.connect()
client.send_notification('Remote', 'connected')
lastPing = time.time()
while True:
try:
dev = InputDevice('/dev/input/event0')
except (FileNotFoundError, PermissionError):
continue
try:
r, w, x = select.select([dev], [], [], .1)
if r:
for event in dev.read_loop():
if event.type == ecodes.EV_KEY:
if event.value == 1:# or event.value == 2:
action = None
# print('Got an event')
if event.code == ecodes.KEY_LEFT:
action = 'Left'
elif event.code == ecodes.KEY_RIGHT:
action = 'Right'
elif event.code == ecodes.KEY_UP:
action = 'Up'
elif event.code == ecodes.KEY_DOWN:
action = 'Down'
elif event.code == ecodes.KEY_BACKSPACE:
action = 'Back'
elif event.code == ecodes.KEY_ENTER:
action = 'Select'
elif event.code == ecodes.KEY_SPACE:
action = 'Pause'
elif event.code == ecodes.KEY_MINUS:
action = 'SetProperty(cec_redirect, voldown 5, 10000)'
elif event.code == ecodes.KEY_EQUAL:
action = 'SetProperty(cec_redirect, volup 5, 10000)'
elif event.code == ecodes.KEY_S:
action = 'RunScript(script.powerkey)'
elif event.code == ecodes.KEY_C:
action = 'ContextMenu'
elif event.code == ecodes.KEY_X:
action = 'Stop'
elif event.code == ecodes.KEY_P:
action = 'Play'
elif event.code == ecodes.KEY_R:
action = 'Rewind'
elif event.code == ecodes.KEY_F:
action = 'Forward'
elif event.code == ecodes.KEY_COMMA:
action = 'ChapterOrBigStepBack'
elif event.code == ecodes.KEY_DOT:
action = 'ChapterOrBigStepForward'
elif event.code == ecodes.KEY_0:
action = 'Number0'
elif event.code == ecodes.KEY_1:
action = 'Number1'
elif event.code == ecodes.KEY_2:
action = 'Number2'
elif event.code == ecodes.KEY_3:
action = 'Number3'
elif event.code == ecodes.KEY_4:
action = 'Number4'
elif event.code == ecodes.KEY_5:
action = 'Number5'
elif event.code == ecodes.KEY_6:
action = 'Number6'
elif event.code == ecodes.KEY_7:
action = 'Number7'
elif event.code == ecodes.KEY_8:
action = 'Number8'
elif event.code == ecodes.KEY_9:
action = 'Number9'
else:
print('Unknown key: ' + ecodes.KEY[event.code])
if action != None:
client.send_action(actionmessage=action, actiontype=ACTION_BUTTON)
except OSError:
pass
except IOError:
pass
if time.time() > lastPing + 30:
# Keep connection alive (will timeout if we remain quite for 60 seconds):
client.ping()
lastPing = time.time()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment