import threading import json from select import select from evdev import ecodes, InputDevice class Keyboard(threading.Thread): def __init__(self,next,prev,toggle): super(Keyboard, self).__init__() self.running = False self.prev = prev self.next = next self.toggle = toggle def run(self): self.running=True self.handleInput() def handleInput(self): device="/dev/input/by-id/usb-Microsoft_Comfort_Curve_Keyboard_3000-event-kbd" device="/dev/input/by-id/usb-_USB_Keyboard-event-kbd" try: dev = InputDevice(device) except Exception as e: print("Error accessing input device %s" % device) self.running=False return #dev = InputDevice("/dev/input/by-path/platform-i8042-serio-0-event-kbd") VALID_KEYS = [ "KEY_PAGEUP", "KEY_PAGEDOWN", "KEY_F5" ] print("Reading keyboard") while True: r, w, e = select([dev], [], []) for ev in dev.read(): codename = '?' if ev.type == ecodes.EV_SYN: continue if ev.value != 0: continue if ev.type in ecodes.bytype: codename = ecodes.bytype[ev.type][ev.code] if codename == "KEY_PAGEUP": self.prev() if codename == "KEY_PAGEDOWN": self.next() if codename == "KEY_F5": self.toggle()