def readLoop(self): device = evdev.InputDevice(CONFIG.get(CONFIG_SECTION, CONFIG_DEVPATH)) self.device = device logger.info(self.__class__.__name__ + " opening device: " + str(device)) device.grab() logger.info(self.__class__.__name__ + " opened device: " + str(device)) try: for event in device.read_loop(): if event.type == evdev.ecodes.EV_KEY: if evdev.categorize(event) == None: continue logger.info(self.__class__.__name__ + " input: " + str(evdev.categorize(event))) keyevent = evdev.KeyEvent(event) if keyevent.keystate in ( 1, ): #state: 0=up, 1=down, 2=hold keycode = keyevent.keycode self.keydispatcher.dispatch(keycode) #print("scancode: ", evdev.KeyEvent(event).scancode, "keycode:", evdev.KeyEvent(event).keycode) #print("event:", evdev.categorize(event)) except: type, value, traceback = sys.exc_info() logger.info(self.__class__.__name__ + " Error: " + str(value) + "\n" + str(traceback))
def keyboard_check_updates(keyboard_obj): event = keyboard_obj.read_one() if event and event.type == evdev.ecodes.EV_KEY: key = evdev.KeyEvent(event) if key.keystate == evdev.KeyEvent.key_down: key_lookup = scancodes.get(key.scancode) or 'INVALID' if key_lookup in string.ascii_uppercase and not get_cap_state( keyboard_obj): key_lookup = key_lookup.lower() return key_lookup return None
def search_mode(uids): pause() selected = sorted(uids.values()) if len(selected) == 1: speak(["no_other_artists_found"]) toggleplay() return speak(["search_mode"]) UID = "" while len(selected) > 1: r, w, x = select(remotes, [], []) for fd in r: for event in remotes[fd].read(): if event.type == evdev.ecodes.EV_KEY: keyevent = evdev.KeyEvent(event) print keyevent.keycode if keyevent.keycode in VALIDSEARCHKEYS: if keyevent.keystate == 1: keynum = keyevent.keycode.split("_")[1] speak([keynum]) UID = UID + keynum elif keyevent.keycode == "KEY_ENTER": speak(["clearing_playlist"]) clear() if len(selected) <= 10: speak(["adding_artists"] + [ "artists/" + x.replace(' ', '_') for x in selected ]) else: speak(["adding_more_than_10_artists"]) for a in selected: exec_mpd("client.findadd('artist','%s')" % a) toggleplay() return elif keyevent.keycode == "KEY_BACKSPACE": speak(["returning to current playlist"]) toggleplay() return selected = sorted( [uids[k] for k in uids.keys() if k.startswith(UID)]) if len(selected) > 0: speak(["clearing_playlist"]) clear() speak(["adding_artist", "artists/" + selected[0].replace(' ', '_')]) exec_mpd("client.findadd('artist','%s')" % selected[0]) else: speak(["artist_not_found"]) toggleplay() return
def listen_mode(): while True: r, w, x = select(remotes, [], []) for fd in r: for event in remotes[fd].read(): if event.type == evdev.ecodes.EV_KEY: keyevent = evdev.KeyEvent(event) print keyevent.keycode if keyevent.keycode in VALIDKEYCODES: if keyevent.keycode == "KEY_D" and keyevent.keystate == 1: # info key info() elif keyevent.keycode == "KEY_PLAYPAUSE" and keyevent.keystate == 1: # play/pause key toggleplay() elif keyevent.keycode == "KEY_F3" and keyevent.keystate == 1: # red key togglerandom() elif keyevent.keycode == "KEY_F6" and keyevent.keystate == 1: # blue key togglerepeat() elif keyevent.keycode == "KEY_STOPCD" and keyevent.keystate == 1: # stop key stop() elif keyevent.keycode == "KEY_B" and keyevent.keystate in [ 1, 2 ]: # rw key skipbackward(SKIPSIZE) elif keyevent.keycode == "KEY_F" and keyevent.keystate in [ 1, 2 ]: # ff key skipforward(SKIPSIZE) elif keyevent.keycode in [ "KEY_PREVIOUSSONG", "KEY_LEFT" ] and keyevent.keystate == 1: # previous key previous() elif keyevent.keycode in [ "KEY_NEXTSONG", "KEY_RIGHT" ] and keyevent.keystate == 1: # next key next() elif keyevent.keycode == "KEY_UP" and keyevent.keystate == 1: # up key next_album() elif keyevent.keycode == "KEY_DOWN" and keyevent.keystate == 1: # down key previous_album() elif keyevent.keycode == "KEY_PAGEDOWN" and keyevent.keystate == 1: # ch- key previous_artist() elif keyevent.keycode == "KEY_PAGEUP" and keyevent.keystate == 1: # ch+ key next_artist() elif keyevent.keycode == "KEY_HOME" and keyevent.keystate == 1: # home key home() elif keyevent.keycode == "KEY_F4" and keyevent.keystate == 1: return
def read_from_device(devname, handler): # Read keyboard events from the device with the given name. # # Args: # devname(str): The name of the device to read from, e.g. '/dev/input/event10'. # handler(func): Handler function that will be called for each read key event. # # Raises: # IOError: If no more events can be read, e.g. if the scale is unplugged. device = evdev.InputDevice(devname) logging.info('Exclusively grabbing device %s', devname) device.grab() logging.info('Starting to read keyboard events from device %s', devname) for event in device.read_loop(): if event.type == evdev.ecodes.EV_KEY: handler(evdev.KeyEvent(event))
def main_loop(): # handle inputs if selector: global keys, clicks for key, mask in selector.select(1): # TODO: more efficient timeout input = key.fileobj for ev in input.read(): if ev.type == evdev.ecodes.EV_KEY: keyev = evdev.KeyEvent(ev) # turn into KeyEvent if keyev.keystate == evdev.KeyEvent.key_up: if ev.code < 255: # arbitary limit keys += 1 else: clicks += 1 else: time.sleep(5) # handle interfaces global prev_bytes, total_bytes cur_bytes = get_bytes() for interface, bytes in cur_bytes.items(): diff = { 'rx': bytes['rx'] - prev_bytes[interface]['rx'], 'tx': bytes['tx'] - prev_bytes[interface]['tx'] } # handle stat counter reset if diff['rx'] < 0: diff['rx'] += pow(2, 32) # is rollever always at 2^32? if diff['tx'] < 0: diff['tx'] += pow(2, 32) total_bytes['rx'] += diff['rx'] total_bytes['tx'] += diff['tx'] prev_bytes = cur_bytes # handle time global prev_time, total_time cur_time = time.time() total_time += cur_time - prev_time prev_time = cur_time autopulse() autostate()
async def clear_on_timeout(self, device_wrapper, key_event, timeout): while key_event.scancode in device_wrapper.input_device.active_keys(): await asyncio.sleep(timeout) await asyncio.sleep(timeout) key_event_up = device_wrapper.last_pressed_up.get(key_event.scancode) if key_event_up is not None and key_event_up.event.timestamp() >= key_event.event.timestamp(): # Above, it needs to be >= because repeat and up fire at the same epoch return # key_event_repeat = device_wrapper.last_pressed_repeat.get(key_event.scancode) # if key_event_repeat is not None and key_event_repeat.event.timestamp() > key_event.event.timestamp(): # return logging.warn(f"Scancode {key_event.scancode} did not go up in time, lifting it.") new_event = evdev.KeyEvent(key_event.event) new_event.keystate = new_event.key_up self.event_queue.put((device_wrapper.input_device, new_event)) self.event_queue.put(HistoryException(f"{evdev.ecodes.KEY[key_event.scancode]} did not go up in time, lifting it."))
#!/usr/bin/env python import os import sys import evdev device = evdev.InputDevice(os.environ['INPUT_DEVICE']) buffer = [] for event in device.read_loop(): if event.type == evdev.ecodes.EV_KEY: event = evdev.KeyEvent(event) # We only want when the key is pressed, not released if event.keystate == evdev.KeyEvent.key_down: # Split something like "KEY_0" and get just the end value = event.keycode.split("_")[-1] if value == "ENTER": print("".join(buffer)) sys.stdout.flush() buffer = [] else: buffer.append(value)