Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # This script takes keyboard input and uses it to send events to the Kodi eventserver,
  4. # it can be installed on any small pc like a Pi.
  5. # To use an ordinary remote one could a Flirc.
  6. # (the eventserver will be started automaticly when Kodi starts)
  7. # It is assumed the keyboard is at /dev/input/event0
  8. # The IP address connected to need to be adjusted to your Kodi installation
  9. # The XBMCClient class is used and can be found in the official Kodi repo
  10. # To get the evdev library you'll need pip: apt-get install python-pip python dev
  11. # and install the library: pip install evdev
  12.  
  13. from evdev import InputDevice, categorize, ecodes
  14. from subprocess import call
  15. import select
  16. import time
  17. from xbmcclient import XBMCClient
  18. from xbmcclient import ACTION_BUTTON
  19.  
  20. client = XBMCClient(ip='192.168.x.x')
  21. client.connect()
  22. client.send_notification('Remote', 'connected')
  23.  
  24. lastPing = time.time()
  25.  
  26. while True:
  27. try:
  28. dev = InputDevice('/dev/input/event0')
  29. except (FileNotFoundError, PermissionError):
  30. continue
  31.  
  32. try:
  33. r, w, x = select.select([dev], [], [], .1)
  34. if r:
  35. for event in dev.read_loop():
  36. if event.type == ecodes.EV_KEY:
  37. if event.value == 1:# or event.value == 2:
  38. action = None
  39. # print('Got an event')
  40. if event.code == ecodes.KEY_LEFT:
  41. action = 'Left'
  42. elif event.code == ecodes.KEY_RIGHT:
  43. action = 'Right'
  44. elif event.code == ecodes.KEY_UP:
  45. action = 'Up'
  46. elif event.code == ecodes.KEY_DOWN:
  47. action = 'Down'
  48. elif event.code == ecodes.KEY_BACKSPACE:
  49. action = 'Back'
  50. elif event.code == ecodes.KEY_ENTER:
  51. action = 'Select'
  52. elif event.code == ecodes.KEY_SPACE:
  53. action = 'Pause'
  54. elif event.code == ecodes.KEY_MINUS:
  55. action = 'SetProperty(cec_redirect, voldown 5, 10000)'
  56. elif event.code == ecodes.KEY_EQUAL:
  57. action = 'SetProperty(cec_redirect, volup 5, 10000)'
  58. elif event.code == ecodes.KEY_S:
  59. action = 'RunScript(script.powerkey)'
  60. elif event.code == ecodes.KEY_C:
  61. action = 'ContextMenu'
  62. elif event.code == ecodes.KEY_X:
  63. action = 'Stop'
  64. elif event.code == ecodes.KEY_P:
  65. action = 'Play'
  66. elif event.code == ecodes.KEY_R:
  67. action = 'Rewind'
  68. elif event.code == ecodes.KEY_F:
  69. action = 'Forward'
  70. elif event.code == ecodes.KEY_COMMA:
  71. action = 'ChapterOrBigStepBack'
  72. elif event.code == ecodes.KEY_DOT:
  73. action = 'ChapterOrBigStepForward'
  74. elif event.code == ecodes.KEY_0:
  75. action = 'Number0'
  76. elif event.code == ecodes.KEY_1:
  77. action = 'Number1'
  78. elif event.code == ecodes.KEY_2:
  79. action = 'Number2'
  80. elif event.code == ecodes.KEY_3:
  81. action = 'Number3'
  82. elif event.code == ecodes.KEY_4:
  83. action = 'Number4'
  84. elif event.code == ecodes.KEY_5:
  85. action = 'Number5'
  86. elif event.code == ecodes.KEY_6:
  87. action = 'Number6'
  88. elif event.code == ecodes.KEY_7:
  89. action = 'Number7'
  90. elif event.code == ecodes.KEY_8:
  91. action = 'Number8'
  92. elif event.code == ecodes.KEY_9:
  93. action = 'Number9'
  94. else:
  95. print('Unknown key: ' + ecodes.KEY[event.code])
  96. if action != None:
  97. client.send_action(actionmessage=action, actiontype=ACTION_BUTTON)
  98. except OSError:
  99. pass
  100. except IOError:
  101. pass
  102.  
  103. if time.time() > lastPing + 30:
  104. # Keep connection alive (will timeout if we remain quite for 60 seconds):
  105. client.ping()
  106. lastPing = time.time()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement