keyboard.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import threading
  2. import json
  3. from select import select
  4. from evdev import ecodes, InputDevice
  5. class Keyboard(threading.Thread):
  6. def __init__(self,next,prev,toggle):
  7. super(Keyboard, self).__init__()
  8. self.running = False
  9. self.prev = prev
  10. self.next = next
  11. self.toggle = toggle
  12. def run(self):
  13. self.running=True
  14. self.handleInput()
  15. def handleInput(self):
  16. device="/dev/input/by-id/usb-Microsoft_Comfort_Curve_Keyboard_3000-event-kbd"
  17. device="/dev/input/by-id/usb-_USB_Keyboard-event-kbd"
  18. try:
  19. dev = InputDevice(device)
  20. except Exception as e:
  21. print("Error accessing input device %s" % device)
  22. self.running=False
  23. return
  24. #dev = InputDevice("/dev/input/by-path/platform-i8042-serio-0-event-kbd")
  25. VALID_KEYS = [ "KEY_PAGEUP", "KEY_PAGEDOWN", "KEY_F5" ]
  26. print("Reading keyboard")
  27. while True:
  28. r, w, e = select([dev], [], [])
  29. for ev in dev.read():
  30. codename = '?'
  31. if ev.type == ecodes.EV_SYN:
  32. continue
  33. if ev.value != 0:
  34. continue
  35. if ev.type in ecodes.bytype:
  36. codename = ecodes.bytype[ev.type][ev.code]
  37. if codename == "KEY_PAGEUP":
  38. self.prev()
  39. if codename == "KEY_PAGEDOWN":
  40. self.next()
  41. if codename == "KEY_F5":
  42. self.toggle()